0

我有一个自定义的异常类,我想在发生异常时将其作为 json 返回。

SpringCacheException.java

@JsonSerialize
public class SpringCacheException extends Exception{
    private static final long serialVersionUID = 1L;
    private HttpStatus status;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
    private LocalDateTime timestamp;
    private String message;
    private String debugMessage;
    public SpringCacheException(HttpStatus status, String message, String debugMessage) {
        super();
        this.setStatus(status);
        this.message = message;
        this.debugMessage = debugMessage;
    }
    public SpringCacheException() {
        timestamp = LocalDateTime.now();
    }

    public SpringCacheException(HttpStatus status) {
        this();
        this.setStatus(status);
    }

    public SpringCacheException(HttpStatus status, Throwable ex) {
        this();
        this.setStatus(status);
        this.setMessage("Unexpected error");
        this.setDebugMessage(ex.getLocalizedMessage());
    }

    public SpringCacheException(HttpStatus status, String message, Throwable ex) {
        this();
        this.setStatus(status);
        this.setMessage(message);
        this.setDebugMessage(ex.getLocalizedMessage());
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDebugMessage() {
        return debugMessage;
    }

    public void setDebugMessage(String debugMessage) {
        this.debugMessage = debugMessage;
    }
    public HttpStatus getStatus() {
        return status;
    }
    public void setStatus(HttpStatus status) {
        this.status = status;
    }
}

RestExceptionHandler.java

 @Order(Ordered.HIGHEST_PRECEDENCE)
    @ControllerAdvice
    public class RestExceptionHandler extends ResponseEntityExceptionHandler {
        private static final Logger logger= LoggerFactory.getLogger(ContactServiceImpl.class);

         @Override
            protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
                SpringCacheException errorMessage = new SpringCacheException(status.BAD_REQUEST,"My Error",ex);
                return new ResponseEntity<Object>(errorMessage, headers, status);
            }

@ExceptionHandler(SpringCacheException.class)
        ResponseEntity<Object> handleBadRequests(HttpStatus status,String message,SpringCacheException ex) throws IOException  {
            SpringCacheException errorMessage = new SpringCacheException(HttpStatus.BAD_REQUEST,"My Error",ex);
            return new ResponseEntity<Object>(errorMessage,HttpStatus.BAD_REQUEST);
        }
    }

抛出异常:-

@Override
    public Contact show(int id) throws SpringCacheException  {
        try {
            Contact contact = contactRepository.findOneById(id);
            return contact;
        }
        catch (Exception e) {
            throw new SpringCacheException();
        }
    }

在这里,我从数据库中删除联系人表并引发异常,我得到以下长响应。

     Position: 111
2017-10-22 00:37:08.577  WARN 10336 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Failed to invoke @ExceptionHandler method: org.springframework.http.ResponseEntity<java.lang.Object> com.myapp.exception.RestExceptionHandler.handleBadRequests(org.springframework.http.HttpStatus,java.lang.String,com.myapp.exception.SpringCacheException) throws java.io.IOException

我只想要一个 SpringCacheException 类型的 json,但我得到的这个长 json 数据不正确。谁能告诉我我在哪里做错了。谢谢 !

整个代码可以在 - https://github.com/iftekharkhan09/SpringCaching找到

4

1 回答 1

1

您的异常处理程序方法需要 SpringCacheException 对象的单个参数(其注释中的类)。

@ExceptionHandler(SpringCacheException.class)
        ResponseEntity<Object> handleBadRequests(SpringCacheException ex) throws IOException  {
            //SpringCacheException errorMessage = new SpringCacheException(HttpStatus.BAD_REQUEST,"My Error",ex);
            return new ResponseEntity<Object>(ex, HttpStatus.BAD_REQUEST);
        }
    }

顺便说一句,创建一个新的 SpringCacheException 违背了该处理程序的目的。您应该将传入的异常返回给方法

我将您的项目分叉并在此处修复:https ://github.com/olantobi/SpringCaching

于 2017-10-21T21:42:15.117 回答