Spring Boot Sping Boot 验证程序不返回自定义错误消息

bbmckpt7  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(169)

我想比较我的实体中的两个字段,并检查maxField是否大于minField有效。下面是我的实现,但我看不到自定义消息,而是返回400 bad request:

  1. {
  2. "errors": []
  3. }

字符串
我的dto:

  1. @ValidMaxMinDouble(
  2. maxFieldName = "maxOperatingTemperature",
  3. minFieldName = "minOperatingTemperature",
  4. message =
  5. "Maximum operating temperature must not be less than the minimum operating temperature")
  6. @Data
  7. public class RequestDTO {
  8. @ValidDoubleRange(min = -100, max = 100, required = true)
  9. private Double maxOperatingTemperature;
  10. @ValidDoubleRange(min = -100, max = 100, required = true)
  11. private Double minOperatingTemperature;
  12. }


这是我的validator:

  1. @Documented
  2. @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,ElementType.TYPE})
  3. @Retention(RetentionPolicy.RUNTIME)
  4. @Constraint(validatedBy = ValidMaxMinDoubleValidator.class)
  5. public @interface ValidMaxMinDouble {
  6. String message() default "The maximum value must be greater than or equal to the minimum value";
  7. Class<?>[] groups() default {};
  8. Class<? extends Payload>[] payload() default {};
  9. String maxFieldName();
  10. String minFieldName();
  11. @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,ElementType.TYPE})
  12. @Retention(RetentionPolicy.RUNTIME)
  13. @interface List {
  14. ValidMaxMinDouble[] value();
  15. }
  16. }
  17. class ValidMaxMinDoubleValidator implements ConstraintValidator<ValidMaxMinDouble, Object> {
  18. private String maxFieldName;
  19. private String minFieldName;
  20. private String message;
  21. @Override
  22. public void initialize(ValidMaxMinDouble constraintAnnotation) {
  23. this.maxFieldName = constraintAnnotation.maxFieldName();
  24. this.minFieldName = constraintAnnotation.minFieldName();
  25. this.message = constraintAnnotation.message();
  26. }
  27. @Override
  28. public boolean isValid(Object value, ConstraintValidatorContext context) {
  29. Double maxValue = (Double) new BeanWrapperImpl(value).getPropertyValue(maxFieldName);
  30. Double minValue = (Double) new BeanWrapperImpl(value).getPropertyValue(minFieldName);
  31. if (maxValue == null || minValue == null) {
  32. return false;
  33. }
  34. boolean valid = maxValue >= minValue;
  35. if (!valid && !message.isEmpty()) {
  36. context.disableDefaultConstraintViolation();
  37. context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
  38. }
  39. return valid;
  40. }
  41. }


我不知道为什么我看不到自定义错误消息。我发送的请求触发context.disabledisableDefaultConstraintViolation()部分。

ep6jt1vc

ep6jt1vc1#

我在这里复制https://github.com/sbernardo/spring-issues-examples/tree/main/sof-questions-77724410的情况下。
文档pom.xml:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- Validation -->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-validation</artifactId>
  10. </dependency>
  11. <!-- Swagger UI for test -->
  12. <dependency>
  13. <groupId>org.springdoc</groupId>
  14. <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  15. <version>${springdoc-openapi.version}</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.projectlombok</groupId>
  19. <artifactId>lombok</artifactId>
  20. <scope>provided</scope>
  21. </dependency>
  22. </dependencies>

字符串
我还添加了一个@ControllerAdvice来拦截所有错误,并返回一个带有自定义消息的BadRequest。您可以自定义响应结构。
让我知道如果错过了什么,希望这将有助于;)

展开查看全部

相关问题