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

Custom validation and javax.validation.constraints doesnt work

我实现了自己的验证来检查我的 SpringApp 中预算实体的容量值。代码如下:

注释:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Constraint(validatedBy = {CapacityValidator.class})
public @interface Capacity {

String message() default "Capacity over limit. Maximum value 9999";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

容量验证器class:

import com.example.CapacityGurdian.annotation.Capacity;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class CapacityValidator implements ConstraintValidator<Capacity, Float> {

private final static float MAX_CAPACITY_VALUE = 9999;

@Override
public boolean isValid(Float value, ConstraintValidatorContext constraintValidatorContext) {
    return MAX_CAPACITY_VALUE < value;
 }
}

然后我像这样在 REST 控制器中使用它:

@RestController
@RequestMapping("/budgets")
@Validated
@Api(
    value = "GrantsBudgetController",
    tags = "Capacity controller for budget"
)
public class BudgetController {

BudgetService budgetService;

@Autowired
public BudgetController(BudgetService budgetService) {
  
    this.budgetService = budgetService;
}

@PutMapping({"/budget/{id}/capacity/{value}"})
@ApiOperation(value = "Updates capacity od specified budget by id",
        notes = "Adds value to capacity of specified budget",
        response = Budget.class)
public ResponseEntity<Budget> updateBudget(
        @ApiParam(value = "Id of updated budget") @PathVariable @NotNull Long id,
        @ApiParam(value = "Value which will be added to current capacity") @PathVariable 
@Capacity Float value) {
    return ResponseEntity.ok(budgetService.addCapacityById(id,value));
}

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

pom.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>CapacityGurdian</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CapacityGurdian</name>
<description>CapacityGuardianApp</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
        <version>2.7.2</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

<!--        JPA - DATABASE-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
    </dependency>
</dependencies>

<build>
    <plugins>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <addResources>true</addResources>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
        </plugin>

    </plugins>

</build>
</project>

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

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
</dependency>