[英]Spring Boot @ExceptionHandler hide Exception Name
I am using Spring Boot 1.3.X and have the following: 我正在使用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
}
}
Which works as expected. 哪个按预期工作。 When passing a wrong param, the following exception is raised: 传递错误的参数时,会引发以下异常:
{
"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
}
I then created an ExceptionHandler
as seen below: 然后我创建了一个ExceptionHandler
,如下所示:
@ControllerAdvice
public class ExcptionController {
@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Invalid parameter")
@ExceptionHandler(UnsatisfiedServletRequestParameterException.class)
private void foo() {
//Log exception
}
}
Which raises the following Exception: 这引发了以下异常:
{
"error": "Bad Request",
"exception": "org.springframework.web.bind.UnsatisfiedServletRequestParameterException",
"message": "Invalid parameter",
"path": "/api/foo",
"status": 400,
"timestamp": 1455287904886
}
Is it possible to exclude the exception field from the JSON
representation? 是否可以从JSON
表示中排除异常字段?
You can get the Error Attributes in your controller advice and then, only keep the Exposable Fields . 您可以在控制器建议中获取错误属性 ,然后只保留Exposable Fields 。 Something like following: 如下:
@ControllerAdvice
public class ExcptionController {
private static final List<String> EXPOSABLE_FIELDS = asList("timestamp", "status", "error", "message", "path");
@Autowired private ErrorAttributes errorAttributes;
@ExceptionHandler(UnsatisfiedServletRequestParameterException.class)
private ResponseEntity foo(HttpServletRequest request) {
Map<String, Object> errors = getErrorAttributes(request);
errors.put("message", "Invalid parameter");
return ResponseEntity.badRequest().body(errors);
}
private Map<String, Object> getErrorAttributes(HttpServletRequest request) {
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
final boolean WITHOUT_STACK_TRACE = false;
Map<String, Object> attributes = errorAttributes.getErrorAttributes(requestAttributes, WITHOUT_STACK_TRACE);
// log exception before removing it
attributes.keySet().removeIf(key -> !EXPOSABLE_FIELDS.contains(key));
return attributes;
}
}
I have solved this bean-style. 我已经解决了这种豆式的问题。 Adding the following bean definition to REST layer configuration replacing ErrorAttributes
used solved issue in my case without any runtime code on exception processing. 将以下bean定义添加到REST层配置替换ErrorAttributes
使用已解决的问题,在我的情况下没有任何运行时代码进行异常处理。
@Bean
public ErrorAttributes errorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(
RequestAttributes requestAttributes,
boolean includeStackTrace) {
Map<String, Object> errorAttributes
= super.getErrorAttributes(requestAttributes, includeStackTrace);
errorAttributes.remove("exception");
return errorAttributes;
}
};
}
For those who use spring boot 2.x. 对于那些使用弹簧靴2.x.
Since version 2.0.0 default implemetation of ErrorAttrubutes is DefaultErrorAttributes . 从版本2.0.0开始,ErrorAttrubutes的默认实现是DefaultErrorAttributes 。
When possible DefaultErrorAttributes provides exception stack trace. 如果可能,DefaultErrorAttributes提供异常堆栈跟踪。 Deleting this field from the answer is possible by setting: 通过设置,可以从答案中删除此字段:
server.error.include-stacktrace: never
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.