Spring Boot @ExceptionHandler隐藏异常名称

tpl*_*cht 7 java spring spring-mvc spring-boot

我正在使用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
    }

}
Run Code Online (Sandbox Code Playgroud)

哪个按预期工作.传递错误的参数时,会引发以下异常:

{
    "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
}
Run Code Online (Sandbox Code Playgroud)

然后我创建了ExceptionHandler如下所示:

@ControllerAdvice
public class ExcptionController {

    @ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Invalid parameter")
    @ExceptionHandler(UnsatisfiedServletRequestParameterException.class)
    private void foo() {
        //Log exception
    }
}
Run Code Online (Sandbox Code Playgroud)

这引发了以下异常:

{
    "error": "Bad Request", 
    "exception": "org.springframework.web.bind.UnsatisfiedServletRequestParameterException", 
    "message": "Invalid parameter", 
    "path": "/api/foo", 
    "status": 400, 
    "timestamp": 1455287904886
}
Run Code Online (Sandbox Code Playgroud)

是否可以从表示中排除异常字段JSON

Ali*_*ani 8

您可以在控制器建议中获取错误属性,然后只保留Exposable Fields.如下:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

对于那些使用 spring boot 2.x 的人。
自 2.0.0 版以来, ErrorAttrubutes 的默认实现是DefaultErrorAttributes
如果可能, DefaultErrorAttributes 提供异常堆栈跟踪。可以通过设置从答案中删除此字段:

server.error.include-stacktrace: never
Run Code Online (Sandbox Code Playgroud)


Rom*_*nko 5

我已经解决了这种豆式的问题.将以下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;
        }

    };
}
Run Code Online (Sandbox Code Playgroud)