Spring 如何在@ExceptionHandler方法中检索请求后对象/参数
我有一个controllerAdvice/@ExceptionHandler方法。我需要在ExceptionHandler方法中访问我的请求参数。 我可以用getRequestURI()检索URI,但不知道如何检索POST请求参数 我有一些标识符参数,必须与ExceptionResponse一起发送回去。我该怎么做Spring 如何在@ExceptionHandler方法中检索请求后对象/参数,spring,rest,Spring,Rest,我有一个controllerAdvice/@ExceptionHandler方法。我需要在ExceptionHandler方法中访问我的请求参数。 我可以用getRequestURI()检索URI,但不知道如何检索POST请求参数 我有一些标识符参数,必须与ExceptionResponse一起发送回去。我该怎么做 @ExceptionHandler(HttpClientErrorException.class) @ResponseStatus(value = HttpStatu
@ExceptionHandler(HttpClientErrorException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody MyResponse handleResourceNotFound(final HttpClientErrorException ex,
final HttpServletRequest request) {
String str = request.getRequestURI();
System.out.println(str);
MyResponse response = new MyResponse(ex.getResponseBodyAsString(), 0, "-1");
return response;
}
这方面没有直接的方法。在我的情况下,我确实喜欢以下内容 控制器
@PostMapping("/test/")
@ResponseBody
public boolean isExist(@RequestBody Person person, HttpServletRequest request) {
request.setAttribute("requestBody", person);
int i = 1/0;
return person != null && person.isExist();
}
@ExceptionHandler(HttpClientErrorException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody MyResponse handleResourceNotFound(final HttpClientErrorException ex,
final HttpServletRequest request) {
String str = request.getRequestURI();
System.out.println(request.getAttribute("requestBody"));
MyResponse response = new MyResponse(ex.getResponseBodyAsString(), 0, "-1");
return response;
}
异常处理程序
@PostMapping("/test/")
@ResponseBody
public boolean isExist(@RequestBody Person person, HttpServletRequest request) {
request.setAttribute("requestBody", person);
int i = 1/0;
return person != null && person.isExist();
}
@ExceptionHandler(HttpClientErrorException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody MyResponse handleResourceNotFound(final HttpClientErrorException ex,
final HttpServletRequest request) {
String str = request.getRequestURI();
System.out.println(request.getAttribute("requestBody"));
MyResponse response = new MyResponse(ex.getResponseBodyAsString(), 0, "-1");
return response;
}