Spring Boot 验证 POST 方法

Spring boot validation POST method

提问人:GPPiotr 提问时间:9/20/2023 最后编辑:BalusCGPPiotr 更新时间:9/20/2023 访问量:34

问:

我不知道为什么当我专门在邮递员中输入错误的数据时,我的验证大小和 notblank 不起作用。当我在 postman 中执行 POST 方法时,将创建包。问题是什么,我该如何解决?

这是我的json

{
  "startingPoint": {
    "country": "a",
    "city": "ab",
    "postalCode": "00-001"
  },
  "destinationPoint": {
    "city": "Berlin",
    "postalCode": "10115"
  },
  "expectedShippingTime": "2023-09-20T10:00:00.000Z",
  "trackingCode": "12345678901"
}

这是我的包裹

package com.courier.application.model;

import lombok.*;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "packages")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Package {
    @Id
    private ObjectId packageId;
    private Point startingPoint;
    private Point destinationPoint;
    private String expectedShippingTime;
    private String trackingCode;
}

这是我的观点

package com.courier.application.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.NotBlank;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Point {
    @NotBlank
    @Size(min = 2, max = 25)
    private String country;
    @NotBlank
    @Size(min = 2, max = 25)
    private String city;
    @NotBlank
    @Size(min = 3, max = 7)
    private String postalCode;
}

这是我的控制器

package com.courier.application.controller;

import com.courier.application.error.PackageAlreadyExistException;
import com.courier.application.error.PackageBadTrackingCodeException;
import com.courier.application.error.PackageNotFoundException;
import com.courier.application.model.Package;
import com.courier.application.service.PackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.Optional;

@RestController
@Validated
public class PackageController {

    @Autowired
    PackageService packageService;

    @GetMapping("/find-package")
    public Optional<Package> findPackageByTrackingCode(String trackingCode) throws PackageNotFoundException, PackageBadTrackingCodeException {
        return packageService.findByTrackingCode(trackingCode);
    }

    @PostMapping("/create-package")
    public Package createPackage(@Valid @RequestBody Package createdPackage) throws PackageAlreadyExistException, PackageBadTrackingCodeException {
        return packageService.saveCreatedPackage(createdPackage);
    }
}

这是pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.3</version>
        <relativePath/>
    </parent>
    <groupId>com.courier.application</groupId>
    <artifactId>courier-system-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>courier-system-application</name>
    <description>System designed for tracking Your package!</description>
    <properties>
        <java.version>20</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>3.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

我尝试了不同的版本和导入,但它们不起作用。

Spring-Boot 验证 帖子

评论


答:

1赞 anteastra 9/20/2023 #1

它称为嵌套验证。您需要对根对象中的字段进行注释

public class Package {
@Id
private ObjectId packageId;
@Valid
private Point startingPoint;
@Valid
private Point destinationPoint;
private String expectedShippingTime;
private String trackingCode;

}

有关更多详细信息,请查看 https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/?v=5.3#section-object-graph-validation