I have a Spring MVC Controller and an Exception Handler. When an exception occurs, I want the Exception Handler to log all the GET/POST data that was sent in the request. How can this be achieved?
Controller:
@Controller
@RequestMapping("/foo")
public class FooController {
private final FooService fooService;
@PostMapping("/bar")
@ResponseBody
public BarObject doSomething(@RequestBody final FooContext context)
{
return fooService.doSomething(context);
}
}
Exception Handler:
@ControllerAdvice
public class ExceptionController {
private final Logger log = LoggerFactory.getLogger(ExceptionController.class);
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class)
@ResponseBody
public ErrorMessage handleException(final HttpServletRequest request, final Exception exception) {
//Retrieve request data
//request.getQueryString()
// How to get POST data if we cannot access @RequestBody?)
log.error(request.getRequestURI(), exception);
return new ErrorMessage(request.getRequestURI(), exception.getLocalizedMessage());
}