Spring Boot @ExceptionHandler hide exception name

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

It works as expected. When passing the wrong parameter, the following exception occurs:

 { "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 } 

Then I created an ExceptionHandler as shown below:

 @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 an exception field from a JSON view?

+6
source share
3 answers

You can get the error attributes in your controller board and then save only the displayed fields. Something like the 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; } } 
+8
source

I decided this bean style. Adding the following bean definition to the REST level configuration, replacing ErrorAttributes , the used solution to the problem in my case without the execution code during exception handling.

 @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; } }; } 
+5
source

For those who use a spring boot 2.x.
Starting with version 2.0.0, the default implementation of ErrorAttrubutes is DefaultErrorAttributes .
Whenever possible, DefaultErrorAttributes provides an exception stack trace. You can remove this field from the response by setting:

 server.error.include-stacktrace: never 
0
source

Source: https://habr.com/ru/post/1242883/


All Articles