代码之家  ›  专栏  ›  技术社区  ›  PatPanda

使用springboot验证请求中的列表是否为[null]

  •  0
  • PatPanda  · 技术社区  · 4 月前

    我的目标是针对如下请求进行验证(失败):

    {
      "title": "some title",
      "foos": [null]
    }
    

    我有一个简单的代码:

    import jakarta.validation.Valid;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    class FieldValidationController {
    
        @PostMapping("/question")
        String question(@Valid @RequestBody SomeRequest someRequest) {
            return "test";
        }
    
    }
    
    record SomeRequest(
    
        @NotEmpty(message = "Please enter the title")
        String title,
    
        @NotNull
        @NotEmpty
        @Valid
        List<@Valid Foo> foos
    
    ) {
    }
    
    import jakarta.validation.constraints.NotEmpty;
    import jakarta.validation.constraints.NotNull;
    
    import java.util.Map;
    
    @NotNull
    record Foo(@NotNull @NotEmpty String name, @NotNull @NotEmpty Map<String, Object> themap) {
    }
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    class FieldValidationApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(FieldValidationApplication.class, args);
        }
    
    }
    
    <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 http://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>3.4.1</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <artifactId>question</artifactId>
    
        <properties>
            <maven.compiler.source>23</maven.compiler.source>
            <maven.compiler.target>23</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </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-validation</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    我可以确认这是如何工作的,因为在我的5个测试用例中,有4个是好的(失败)。

    1-良好:

    curl --location 'http://localhost:8080/question' \
    --header 'Content-Type: application/json' \
    --data '{
      "title": "some title"
    }'
    

    2-良好:

    curl --location 'http://localhost:8080/question' \
    --header 'Content-Type: application/json' \
    --data '{
      "title": "some title",
      "foos": null
    }'
    

    3-良好:

    curl --location 'http://localhost:8080/question' \
    --header 'Content-Type: application/json' \
    --data '{
      "title": "some title",
      "foos": []
    }'
    

    4-良好:

    curl --location 'http://localhost:8080/question' \
    --header 'Content-Type: application/json' \
    --data '{
      "title": "some title",
      "foos": [{}]
    }'
    

    然而,我预计这也会失败,但事实并非如此。

    curl --location 'http://localhost:8080/question' \
    --header 'Content-Type: application/json' \
    --data '{
      "title": "some title",
      "foos": [null]
    }'
    

    我可以问一下如何核对吗 [null] ?

    1 回复  |  直到 4 月前
        1
  •  2
  •   Georgii Lvov    4 月前

    您需要添加 @NotNull Foo 通用参数:

    record SomeRequest(
    
                @NotEmpty(message = "Please enter the title")
                String title,
    
                @NotNull
                @NotEmpty
                @Valid
                List<@NotNull @Valid Foo> foos // here @NotNull added
    
        ) {
    }