Stack Exchange
Spring boot validation POST method
I don't know why my validation size and notblank do not work when I specially enter wrong data into postman. When I do POST method in postman the package is created. What is the problem and how can I solve it?
This is my json
{
  "startingPoint": {
    "country": "a",
    "city": "ab",
    "postalCode": "00-001"
  },
  "destinationPoint": {
    "city": "Berlin",
    "postalCode": "10115"
  },
  "expectedShippingTime": "2023-09-20T10:00:00.000Z",
  "trackingCode": "12345678901"
}

this is my Package
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;
}

This is my Point
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;
}


this is my controller
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);
    }
}


and this is the 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>

I tried different version and imports, but they did not work.

It is called nested validation. You need to annotate fields in root object
public class Package {
@Id
private ObjectId packageId;
@Valid
private Point startingPoint;
@Valid
private Point destinationPoint;
private String expectedShippingTime;
private String trackingCode;

}
for more details check https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/?v=5.3#section-object-graph-validation
