我使用Spring引導1.3.X和有以下幾點:春季啓動@ExceptionHandler隱藏例外名稱
@RestController
@RequestMapping(path = "/foo")
public class FooController {
@RequestMapping(method = RequestMethod.GET, params = { "fooBar" })
public Collection<Entry> firstFoo() {
//Do something
}
@RequestMapping(method = RequestMethod.GET, params = { "anotherFooBar" })
public Collection<Entry> secondFoo() {
//Do something other
}
}
預期其中一期工程。當傳遞一個錯誤的PARAM,以下異常引發:
{
"error": "Bad Request",
"exception": "org.springframework.web.bind.UnsatisfiedServletRequestParameterException",
"message": "Parameter conditions \"fooBar\" OR \"anotherFooBar\" not met for actual request parameters: elementalFormulae={C11}",
"path": "/foo",
"status": 400,
"timestamp": 1455287433961
}
然後我創建了一個ExceptionHandler
,如下圖所示:
@ControllerAdvice
public class ExcptionController {
@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Invalid parameter")
@ExceptionHandler(UnsatisfiedServletRequestParameterException.class)
private void foo() {
//Log exception
}
}
這就提出了以下異常:
{
"error": "Bad Request",
"exception": "org.springframework.web.bind.UnsatisfiedServletRequestParameterException",
"message": "Invalid parameter",
"path": "/api/foo",
"status": 400,
"timestamp": 1455287904886
}
是它可能會從JSON
表示中排除例外字段?
@JSONIgnore會訣竅嗎? –