spring 引导组件 bean 验证 @Positive 不工作但 @NotNull 工作正常

spring boot component bean validation @Positive not working but @NotNull works fine

我有一个由 spring 管理的服务,我在该服务上添加了 @Validated 注释,参考 this 文章,它告诉 spring 会为我们做验证,我们甚至不需要手动验证它。

import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
@Component
@Validated
public class MyService {
    public void execute(@Valid MyRequest request) {
            //todo
    }
}

MyRequest 是一个 DTO,将由 spring

验证
import lombok.Data;

import javax.validation.constraints.Positive;

@Data
public class MyRequest {
    @Positive(message = "id should be positive")
    private Long id;
}

这是我的控制器,出于某种原因,我不想在控制器中进行验证。

@Controller
public class MainController {
    @Autowired
    private MyService service;
    @RequestMapping(value = "/execute", method = {GET, POST})
    @ResponseBody
    public MyResponse execute() {
        MyResponse res = new MyResponse();
        service.execute(new MyRequest());
        res.setHtmlText("hello world");
        return res;
    }
}

当我访问url时:http://localhost:8001/execute 它没有给我任何异常并显示结果 hello world

这里是maven依赖

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.fudy</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

但是在我将@Positive 替换为@NotNull 之后,它工作正常

import lombok.Data;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@Data
public class MyRequest {
    //@Positive(message = "id should be positive")
    @NotNull(message = "id should not be null")
    private Long id;
}

它正常工作。与 @Positive 关联的验证器,PositiveValidatorForLong 接受 null 值作为有效值。所以它只会验证实际值,而不是 null.

基本上你需要它们来满足你的要求,所以 @Positive@NotNull 都只允许正值。

专业提示: 由于您已经拥有 spring-boot-starter-validation 依赖项,因此不需要额外的 validation-apihibernate-validator 依赖项,这些已经添加了。