本文介绍了Spring Boot @ExceptionHandler 隐藏异常名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Boot 1.3.X 并具有以下内容:

I am using Spring Boot 1.3.X and have the following:

@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
}

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

I then created an ExceptionHandler as seen below:

@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 字段?

Is it possible to exclude the exception field from the JSON representation?

推荐答案

您可以在控制器建议中获取 Error Attributes,然后只保留 Exposable Fields.类似于以下内容:

You can get the Error Attributes in your controller advice and then, only keep the 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;
    }
}

这篇关于Spring Boot @ExceptionHandler 隐藏异常名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:10