我正在使用Spring4开发RESTful应用程序。 我想处理在POST请求中没有传递任何正文的情况。
我写了自定义异常处理程序:
@ControllerAdvice
public class MyRestExceptionHandler {
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity handleJsonMappingException(JsonMappingException ex) {
MyErrorResponse errorResponse = new MyErrorResponse("request has empty body");
return new ResponseEntity(errorResponse, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(Throwable.class)
public ResponseEntity handleDefaultException(Throwable ex) {
MyErrorResponse errorResponse = new MyErrorResponse(ex);
return new ResponseEntity(errorResponse, HttpStatus.BAD_REQUEST);
}
}
@RestController
public class ContactRestController{
@RequestMapping(path="/contact", method=RequestMethod.POST)
public void save(@RequestBody ContactDTO contactDto) {...}
}
但是,当发生这种情况时,将不会调用这些方法。 我刚得到400 BAD REQUEST http状态和空身体的回复。 有人知道如何处理吗?
您是否尝试过@ExceptionHandler(JsonMappingException.class)?
是的 相同的行为。
我发现了如何捕捉它。我的解决方案是:
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
// paste custom hadling here
}
}
它只会为post方法调用吗?
如果您已经有一个用@ControllerAdvice注释的类,并且不想创建新的类,则可以使用以下代码:
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity< ? > handleMissingRequestBody(Exception ex) {
return handle(BAD_REQUEST, ex);
}
它的行为应与rvit34的解决方案相同。
像魅力一样工作。 谢谢 !
我遇到了类似的问题,但它对我不起作用,因为提供的component-scan软件包不包括提供我的@ControllerAdvice的软件包。
我的XML有:
我的包裹有错字com.bandi.test.spring.exception。将其更改为com.bandi.rest.spring.exception后,它开始工作。
@ControllerAdvice
public class SpringRestExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public @ResponseBody ResponseEntity handleNoMethodException(HttpServletRequest request,
NoHandlerFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(ex);
errorResponse.setErrorMessage("resource not found with exception");
return new ResponseEntity(errorResponse, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(Throwable.class)
public @ResponseBody ResponseEntity handleDefaultException(Throwable ex) {
ErrorResponse errorResponse = new ErrorResponse(ex);
errorResponse.setErrorMessage("request has empty body or exception occured");
return new ResponseEntity(errorResponse, HttpStatus.BAD_REQUEST);
}
}
此外,如果需要处理找不到请求的资源(错误的URL)的情况,则必须向调度程序servlet添加另一个配置。
dispatcher
org.springframework.web.servlet.DispatcherServlet
throwExceptionIfNoHandlerFound
true
2
完整的工作代码在这里
我正在使用Spring Boot,所以我不需要做额外的xml配置.Spring会找到所有带注释的bean。 稍后我会检查您的示例
然后可能需要添加基于注释的配置。 如此处所述,还可以验证@ComponentScan批注是否包含ExceptionHandlers包吗?
总的来说,我的ExeptionHandler正在运行。 处理该主题中所有错误。 甚至NoHadler也找到了例外。 我正在使用此建议启用它。
最终找到了解决方案。
您可以发布找到的解决方案吗?
就我而言,我需要处理所有具有无效参数的请求。因此,我使用ResponseEntityExceptionHandler扩展了我的类,并重写了handleMissingServletRequestParameter方法。您可以在类ResponseEntityExceptionHandler中找到自己定义的处理程序
@ControllerAdvice
公共类YourExceptionHandler扩展了ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public final ResponseEntity handleAllExceptions(Exception ex) {
// Log and return
}
@Override
public ResponseEntity handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
// Do your code here
return new ResponseEntity<>("YOUR REQUEST PARAMS NOT MATCH!");
}
}