gpt4 book ai didi

java - 如何在响应正文中返回 ConstraintViolationException 消息?

转载 作者:行者123 更新时间:2023-12-02 09:24:54 26 4
gpt4 key购买 nike

我正在开发一个基于 Spring Boot 的 REST API。我正在使用自定义 ConstraintValidator 注释验证输入实体。我的问题是我无法在响应中返回 ConstraintViolationException 消息。我的异常处理程序没有捕获异常(可能是因为它们被包装在其他类型的异常中)。

我可以得到一些关于如何处理这种情况的建议吗?

我在互联网上进行了搜索,但找不到适合我的解决方案,而且我还浪费了一些时间。

注释示例:

@Documented
@Retention(RUNTIME)
@Target({FIELD, PARAMETER})
@Constraint(validatedBy = BirthDateValidator.class)
public @interface ValidBirthDate {

String message() default "The birth date is not valid.";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}

validator 类:

public class BirthDateValidator extends FieldValidationBase implements ConstraintValidator<ValidBirthDate, LocalDate> {

private static final Logger LOGGER = LoggerFactory.getLogger(BirthDateValidator.class);

@Override
public void initialize(ValidBirthDate constraintAnnotation) {
}

@Override
public boolean isValid(LocalDate birthDate, ConstraintValidatorContext constraintValidatorContext) {
constraintValidatorContext.disableDefaultConstraintViolation();
LOGGER.info("Starting the validation process for birth date {}.", birthDate);

if(birthDate == null) {
constraintValidatorContext.buildConstraintViolationWithTemplate("The birth date is null.")
.addConstraintViolation();
return false;
}

//other validations

return true;
}

模型类:

public class Manager extends BaseUser {

//other fields

@Valid
@ValidBirthDate
private LocalDate birthDate;

//setters & getters

异常处理程序:

@ExceptionHandler(value = ConstraintViolationException.class)
public ResponseEntity handleConstraintViolationException(ConstraintViolationException ex, WebRequest request) {
List<String> errors = new ArrayList<>();

for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
errors.add(violation.getRootBeanClass().getName() + ": " + violation.getMessage());
}

Error response = new Error(errors);
return new ResponseEntity<Object>(response, new HttpHeaders(), BAD_REQUEST);
}

Controller :

@RestController
@RequestMapping(value = "/register", consumes = "application/json", produces = "application/json")
public class RegistrationController {

@Autowired
private RegistrationService registrationService;

@PostMapping(value = "/manager")
public ResponseEntity registerManager(@RequestBody @Valid Manager manager) {
registrationService.executeSelfUserRegistration(manager);
return new ResponseEntity<>(new Message("User " + manager.getEmailAddress() + " registered successfully!"), CREATED);
}
}

我收到 400 响应代码,但没有看到任何包含违反约束消息的响应正文。

最佳答案

经过更多调试后,我发现所有约束违规都被包装到 MethodArgumentNotValidException 中(因为 @Valid 注释) - 我必须深入挖掘一下获取我的信息的异常。

我已经重写了 ResponseEntityExceptionHandler 中的 handleMethodArgumentNotValid() 方法,这就是我让它工作的方式:

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

List<String> errorMessages = new ArrayList<>();
BindingResult bindingResult = ex.getBindingResult();
List<ObjectError> errors = bindingResult.getAllErrors();
for(ObjectError error : errors) {
String message = error.getDefaultMessage();
errorMessages.add(message);
}

return new ResponseEntity<>(new Error(errorMessages), new HttpHeaders(), BAD_REQUEST);
}

也许这对某人有帮助。

关于java - 如何在响应正文中返回 ConstraintViolationException 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399947/

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