@ExceptionHandler(Exception.class) 不处理所有类型的异常
@ExceptionHandler(Exception.class) not handling all types of exceptions
我正在尝试使用 @ExceptionHandler(Exception.class)
处理所有类型的异常。但它并没有处理所有类型的异常。
当我尝试从邮递员/浏览器访问错误的 HTTP 方法时,我没有收到任何响应,空白页面即将到来。
谁能告诉我为什么我没有收到任何回复,或者告诉我我的代码是否做错了什么?
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ExceptionMessage> handleAllExceptionMethod(Exception ex,WebRequest requset,HttpServletResponse res) {
ExceptionMessage exceptionMessageObj = new ExceptionMessage();
exceptionMessageObj.setStatus(res.getStatus());
exceptionMessageObj.setError(ex.getLocalizedMessage());
exceptionMessageObj.setException(ex.getClass().getCanonicalName());
exceptionMessageObj.setPath(((ServletWebRequest) requset).getRequest().getServletPath());
return new ResponseEntity<ExceptionMessage>(exceptionMessageObj, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
这将处理从控制器方法中引发的异常。
如果您发送一个没有映射的请求,则根本不会调用控制器方法,因此在这种情况下 @ExceptionHandler
将过时。
也许这篇关于创建自定义处理程序的文章可能会有所帮助:article
覆盖 ResponseEntityExceptionHandler#handleExceptionInternal()
或不扩展 ResponseEntityExceptionHandler
。
@Order(Ordered.HIGHEST_PRECEDENCE)
在 @ControllerAdvice
上应该在 ResponseEntityExceptionHandler
之前工作,根据 this answer 这表明需要 Spring Framework 4.3.7。
使用 RequestMapping,您可以为每个 Http 代码创建不同的响应。在这个例子中,我展示了如何控制错误并给出相应的响应。
这是具有服务规范的 RestController
@RestController
public class User {
@RequestMapping(value="/myapp/user/{id}", method = RequestMethod.GET)
public ResponseEntity<String> getId(@PathVariable int id){
if(id>10)
throw new UserNotFoundException("User not found");
return ResponseEntity.ok("" + id);
}
@ExceptionHandler({UserNotFoundException.class})
public ResponseEntity<ErrorResponse> notFound(UserNotFoundException ex){
return new ResponseEntity<ErrorResponse>(
new ErrorResponse(ex.getMessage(), 404, "The user was not found") , HttpStatus.NOT_FOUND);
}
}
在 getId 方法中有一点逻辑,如果 customerId < 10 它应该响应 Customer Id 作为正文消息的一部分但是当客户大于 10 时应该抛出异常,在这种情况下服务应以 ErrorResponse 响应。
public class ErrorResponse {
private String message;
private int code;
private String moreInfo;
public ErrorResponse(String message, int code, String moreInfo) {
super();
this.message = message;
this.code = code;
this.moreInfo = moreInfo;
}
public String getMessage() {
return message;
}
public int getCode() {
return code;
}
public String getMoreInfo() {
return moreInfo;
}
}
最后,我对 "Not Found" 错误使用了特定的异常
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}
}
我正在尝试使用 @ExceptionHandler(Exception.class)
处理所有类型的异常。但它并没有处理所有类型的异常。
当我尝试从邮递员/浏览器访问错误的 HTTP 方法时,我没有收到任何响应,空白页面即将到来。
谁能告诉我为什么我没有收到任何回复,或者告诉我我的代码是否做错了什么?
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ExceptionMessage> handleAllExceptionMethod(Exception ex,WebRequest requset,HttpServletResponse res) {
ExceptionMessage exceptionMessageObj = new ExceptionMessage();
exceptionMessageObj.setStatus(res.getStatus());
exceptionMessageObj.setError(ex.getLocalizedMessage());
exceptionMessageObj.setException(ex.getClass().getCanonicalName());
exceptionMessageObj.setPath(((ServletWebRequest) requset).getRequest().getServletPath());
return new ResponseEntity<ExceptionMessage>(exceptionMessageObj, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
这将处理从控制器方法中引发的异常。
如果您发送一个没有映射的请求,则根本不会调用控制器方法,因此在这种情况下 @ExceptionHandler
将过时。
也许这篇关于创建自定义处理程序的文章可能会有所帮助:article
覆盖 ResponseEntityExceptionHandler#handleExceptionInternal()
或不扩展 ResponseEntityExceptionHandler
。
@Order(Ordered.HIGHEST_PRECEDENCE)
在 @ControllerAdvice
上应该在 ResponseEntityExceptionHandler
之前工作,根据 this answer 这表明需要 Spring Framework 4.3.7。
使用 RequestMapping,您可以为每个 Http 代码创建不同的响应。在这个例子中,我展示了如何控制错误并给出相应的响应。
这是具有服务规范的 RestController
@RestController
public class User {
@RequestMapping(value="/myapp/user/{id}", method = RequestMethod.GET)
public ResponseEntity<String> getId(@PathVariable int id){
if(id>10)
throw new UserNotFoundException("User not found");
return ResponseEntity.ok("" + id);
}
@ExceptionHandler({UserNotFoundException.class})
public ResponseEntity<ErrorResponse> notFound(UserNotFoundException ex){
return new ResponseEntity<ErrorResponse>(
new ErrorResponse(ex.getMessage(), 404, "The user was not found") , HttpStatus.NOT_FOUND);
}
}
在 getId 方法中有一点逻辑,如果 customerId < 10 它应该响应 Customer Id 作为正文消息的一部分但是当客户大于 10 时应该抛出异常,在这种情况下服务应以 ErrorResponse 响应。
public class ErrorResponse {
private String message;
private int code;
private String moreInfo;
public ErrorResponse(String message, int code, String moreInfo) {
super();
this.message = message;
this.code = code;
this.moreInfo = moreInfo;
}
public String getMessage() {
return message;
}
public int getCode() {
return code;
}
public String getMoreInfo() {
return moreInfo;
}
}
最后,我对 "Not Found" 错误使用了特定的异常
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}
}