我有一个 Spring MVC 控制器和一个异常处理程序。当发生异常时,我希望异常处理程序记录请求中发送的所有 GET/POST 数据。如何做到这一点?
控制器:
@Controller
@RequestMapping("/foo")
public class FooController {
private final FooService fooService;
@PostMapping("/bar")
@ResponseBody
public BarObject doSomething(@RequestBody final FooContext context)
{
return fooService.doSomething(context);
}
}
异常处理程序:
@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());
}
达令说
相关分类