代码之家  ›  专栏  ›  技术社区  ›  Shashank

spring boot返回400缺少构造函数no stacktrace

  •  0
  • Shashank  · 技术社区  · 6 年前

    我接到一个电话

    public Test postTest(@RequestBody Test test) {
    }
    
    
    public class Test {
        @JsonProperty("EMAIL_ADDRESS")
        @NotNull(message = "EMAIL_ADDRESS_NOT_NULL")
        @Size(min = 1, message = "EMAIL_ADDRESS_NOT_EMPTY")
        @Email(flags = Flag.CASE_INSENSITIVE, message = "EMAIL_INVALID")
        private String emailAddress;
    
        public ForgetPassword(
                @NotNull(message = "EMAIL_ADDRESS_NOT_NULL") @Size(min = 1, 
                message = "EMAIL_ADDRESS_NOT_EMPTY") @Email(flags = 
                Flag.CASE_INSENSITIVE, 
                message = "EMAIL_INVALID") String emailAddress) {
            super();
            this.emailAddress = emailAddress;
        }
    
        public String getEmailAddress() {
            return emailAddress;
        }
    
        public void setEmailAddress(String emailAddress) {
            this.emailAddress = emailAddress;
        }
    }
    

    我错过了默认的构造器,当我试图发帖时,它一直在返回错误的请求。如果我添加默认构造函数,它就会工作。它甚至不会抛出堆栈跟踪。我正在使用自定义模板发送回中的响应。我正在重写badrequestexception以发回自定义消息。

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public class BadRequestException extends RuntimeException {
        public BadRequestException(String message) {
            super(message);
        }
    }
    

    有什么方法我应该重写来捕获这些异常。我想知道为什么出了问题,并且能够看到堆栈的错误。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Gaurav Srivastav    6 年前

    您可以使用 @ControllerAdvice . 您可以使用 @ExcecptionHandler . 您可以记录错误跟踪以进行调试。

    package com.startwithjava.exception;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.MethodArgumentNotValidException;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    
    @ControllerAdvice
    public class ApiExceptionHandler {
        @ExceptionHandler(Exception.class)
        public ResponseEntity<Object> handlerGenericError(Exception ex){
            ex.printStackTrace();
            return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        @ExceptionHandler(BadRequestException.class)
        public ResponseEntity<Object> handlerBadRequest(BadRequestException ex){
            return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
        }
    }
    
        2
  •  1
  •   Alien    6 年前

    您可以像下面这样声明一个类来处理异常。

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseStatus;
    
    @ControllerAdvice
    public class GlobalExceptionHandler {
        private static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    
        @ExceptionHandler(Exception.class)
        @ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR, reason="Error processing the request, please check the logs")
        public void handleException(Exception e) {
            LOGGER.error("Exception occurred", e);
        }
    }