gpt4 book ai didi

java - Spring boot 返回 400 缺少构造函数,没有堆栈跟踪

转载 作者:行者123 更新时间:2023-12-02 01:51:25 26 4
gpt4 key购买 nike

我有一个邮寄电话

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

是否有任何我应该重写的方法来捕获此类异常。我希望能够知道为什么会出现问题并能够看到堆栈跟踪。

最佳答案

您可以使用@ControllerAdvice定义全局异常处理程序。您可以使用@ExcecptionHandler 在那里放置任意数量的处理程序。您可以记录 errorTrace 以进行调试。

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

关于java - Spring boot 返回 400 缺少构造函数,没有堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52934224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com