javax.validation.constraints not working on post request

Viewed 232

I have an entity class that declared constraint validations on fields date & question. Unfortunately the code failed to throw my custom exception InputException when I entered wrong inputs for date and or question via a postman post request. The date field was supposed to accept only four characters starting with either 19 or 20 while question should not accept null or empty String, hence I declared an InitBinder in my controller Entity class:

@Entity
@Table(name = "mathematics")
public class Mathematics {
    
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "date")
@Pattern(regexp = "(19[0-9]{2})|(20[0-9]{2})")
private String date;

@Column(name = "question_no")
private Integer questionNo;

@Column(name = "question")
@NotNull
private String question;

RestController:

@RestController
public class MathController {
    
    private MathService service;
    
    @Autowired
    public MathController(MathService service) {
        
        this.service = service;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        StringTrimmerEditor trimmer = new StringTrimmerEditor(true);
        binder.registerCustomEditor(String.class, trimmer);
    }
    
    @PostMapping("/math")
    public void save( @Valid @RequestBody Mathematics maths,
            HttpServletResponse response, BindingResult theBindingResult) throws IOException{
        
        if(theBindingResult.hasErrors()) {
            throw new InputException("Wrong input detected!");
        }
        
        service.save(maths);
        
        response.sendRedirect("/maths/all");
        
    }

post request:

{
    "questionNo":14,
    "date":"1890",
    "question":"   "
}

    

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.resource</groupId>
    <artifactId>akademiks</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>akademiks</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
0 Answers
Related