【问题标题】:Custom validation and javax.validation.constraints doesnt work自定义验证器和 javax.validation.constraints 不起作用
【发布时间】:2021-07-09 17:31:02
【问题描述】:

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

注释:

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

CapacityValidator 类:

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 中的注释处理器,它设置得很好。有人知道如何打开我的验证吗?

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>

【问题讨论】:

    标签: java spring-boot validation spring-validator


    【解决方案1】:

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

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

    【讨论】:

    • 是的,问题解决了。老实说,我知道为什么在我得到&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt; 之前我需要完全休眠验证器,因为它没有工作;但休眠验证器包含在其中。我在 pom 中添加了 parent,其中包括 hibernate-validator 和 spring-validation,现在它可以工作了
    猜你喜欢
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    相关资源
    最近更新 更多