自定义验证和javax.validation.constraints不起作用

lzfw57am  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(722)

我实施自己的验证,以检查springapp中容量od预算实体的值。代码如下:
注解:

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target({ElementType.FIELD, ElementType.PARAMETER})
  3. @Constraint(validatedBy = {CapacityValidator.class})
  4. public @interface Capacity {
  5. String message() default "Capacity over limit. Maximum value 9999";
  6. Class<?>[] groups() default {};
  7. Class<? extends Payload>[] payload() default {};
  8. }

电容校验器等级:

  1. import com.example.CapacityGurdian.annotation.Capacity;
  2. import javax.validation.ConstraintValidator;
  3. import javax.validation.ConstraintValidatorContext;
  4. public class CapacityValidator implements ConstraintValidator<Capacity, Float> {
  5. private final static float MAX_CAPACITY_VALUE = 9999;
  6. @Override
  7. public boolean isValid(Float value, ConstraintValidatorContext constraintValidatorContext) {
  8. return MAX_CAPACITY_VALUE < value;
  9. }
  10. }

然后我在rest控制器中使用它,如下所示:

  1. @RestController
  2. @RequestMapping("/budgets")
  3. @Validated
  4. @Api(
  5. value = "GrantsBudgetController",
  6. tags = "Capacity controller for budget"
  7. )
  8. public class BudgetController {
  9. BudgetService budgetService;
  10. @Autowired
  11. public BudgetController(BudgetService budgetService) {
  12. this.budgetService = budgetService;
  13. }
  14. @PutMapping({"/budget/{id}/capacity/{value}"})
  15. @ApiOperation(value = "Updates capacity od specified budget by id",
  16. notes = "Adds value to capacity of specified budget",
  17. response = Budget.class)
  18. public ResponseEntity<Budget> updateBudget(
  19. @ApiParam(value = "Id of updated budget") @PathVariable @NotNull Long id,
  20. @ApiParam(value = "Value which will be added to current capacity") @PathVariable
  21. @Capacity Float value) {
  22. return ResponseEntity.ok(budgetService.addCapacityById(id,value));
  23. }

这段代码应该可以工作,但这次不行。然后我决定通过在字符串属性上添加@size(max=3)和@notempty来检查标准javax.validations是否有效。不幸的是,它也不起作用。我在intellij中检查了注解处理器,它设置得很好。有人知道如何打开我的验证吗?
pom.xml的代码:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>CapacityGurdian</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>CapacityGurdian</name>
  9. <description>CapacityGuardianApp</description>
  10. <properties>
  11. <java.version>11</java.version>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-web</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-test</artifactId>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>io.springfox</groupId>
  25. <artifactId>springfox-swagger2</artifactId>
  26. <version>2.9.2</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>io.springfox</groupId>
  30. <artifactId>springfox-swagger-ui</artifactId>
  31. <version>2.9.2</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.kafka</groupId>
  35. <artifactId>spring-kafka</artifactId>
  36. <version>2.7.2</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>javax.validation</groupId>
  40. <artifactId>validation-api</artifactId>
  41. <version>2.0.1.Final</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.projectlombok</groupId>
  45. <artifactId>lombok</artifactId>
  46. <version>1.18.20</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>junit</groupId>
  50. <artifactId>junit</artifactId>
  51. <version>4.12</version>
  52. <scope>test</scope>
  53. </dependency>
  54. <!-- JPA - DATABASE-->
  55. <dependency>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-starter-data-jpa</artifactId>
  58. </dependency>
  59. <dependency>
  60. <groupId>com.h2database</groupId>
  61. <artifactId>h2</artifactId>
  62. <version>1.4.200</version>
  63. </dependency>
  64. </dependencies>
  65. <build>
  66. <plugins>
  67. <plugin>
  68. <groupId>org.springframework.boot</groupId>
  69. <artifactId>spring-boot-maven-plugin</artifactId>
  70. <configuration>
  71. <addResources>true</addResources>
  72. </configuration>
  73. </plugin>
  74. <plugin>
  75. <groupId>org.apache.maven.plugins</groupId>
  76. <artifactId>maven-surefire-plugin</artifactId>
  77. <version>2.22.0</version>
  78. </plugin>
  79. </plugins>
  80. </build>
  81. </project>
jrcvhitl

jrcvhitl1#

您的依赖项不包含 hibernate-validator ,请尝试这样添加:

  1. <dependency>
  2. <groupId>org.hibernate.validator</groupId>
  3. <artifactId>hibernate-validator</artifactId>
  4. </dependency>

相关问题