Javax 验证未对生成的 Immutable 进行 class

Javax validation are not carried to Immutable generated class

我正在使用 immutables.org 库并希望添加 Javax 注释验证。但是我注意到验证没有被带到生成的class。我认为 immutables.org 与验证注释兼容。 我有 spring-boot-starter-validationjavax.validation 依赖关系。

不可变

@Value.Immutable
public interface Entity {
    @Valid @Size(min = 19)
    int age();

    @Valid @NotBlank
    String name();
}

控制器:

    @PostMapping("/entity/")
    public ResponseEntity post2(@RequestBody @Valid ImmutableEntity entity) {


        return  new ResponseEntity<Object>("", HttpStatus.OK);
    }

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>testImmutable</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>testImmutable</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-validation</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.immutables</groupId>
            <artifactId>value</artifactId>
            <version>2.8.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.5</version>
        </dependency>
    </dependencies>

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

</project>
  1. 真的,@Valid 不应该在模型中为字段或 class 设置,只为控制器方法的参数设置。
  2. @Size 不适用于 int 字段,您需要使用 @Range@Min.
  3. 在您的界面中,您注释了抽象方法,而不是字段。验证器如何将验证字段 agename 与方法 age()name() 相关联?当它们被调用(它们的实现)时,验证器将尝试验证它们。但不是模特的领域。