Spring Boot @ExceptionHandler隐藏异常名称

时间:2016-02-12 14:39:46

标签: 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
    }

}

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

{
    "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表示中排除例外字段?

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:5)

我已经解决了这种bean风格。将以下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;
        }

    };
}

答案 2 :(得分:0)

对于使用Spring Boot 2.x的用户。
从2.0.0版开始,ErrorAttrubutes的默认实现为DefaultErrorAttributes
如果可能,DefaultErrorAttributes提供异常堆栈跟踪。通过设置可以从答案中删除该字段:

server.error.include-stackrace: never