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

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

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

@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 {};
}

电容校验器等级:

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

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

jrcvhitl1#

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

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

相关问题