繁体   English   中英

@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);          
        } 

要么覆盖ResponseEntityExceptionHandler#handleExceptionInternal()要么不要扩展ResponseEntityExceptionHandler

@ControllerAdvice上的@Order(Ordered.HIGHEST_PRECEDENCE)应该在根据这个答案调用ResponseEntityExceptionHandler之前工作,这表明需要Spring Framework 4.3.7。

这将处理从控制器方法中引发的异常。

如果发送没有映射的请求,则根本不会调用控制器方法,因此在这种情况下@ExceptionHandler将会过时。

也许这篇关于创建自定义处理程序的文章可能会有所帮助

使用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它应该响应客户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;
    }
}

最后我使用了一个特定的Exception来查找“Not Found”错误

public class UserNotFoundException extends RuntimeException {

    public UserNotFoundException(String message) {
        super(message);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM