【发布时间】:2016-02-12 14:39:46
【问题描述】:
我正在使用 Spring Boot 1.3.X 并具有以下内容:
@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
}
}
按预期工作。传递错误的参数时,会引发以下异常:
{
"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 表示中排除 exception 字段?
【问题讨论】:
-
@JSONIgnore 能解决问题吗?
标签: java spring spring-mvc spring-boot