Skip to content
Advertisement

Why spring validator is not working in this Api?

I have written a basic simple API of students data and added few validations using Hibernate validator but everytime its returning 0 errors.

Here’s the code:

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.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>16</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>7.0.1.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
         

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

student.java

package com.example.demo;

import javax.validation.constraints.NotBlank;

public class student {
    @NotBlank(message = "hey")

    private String name;
    @NotBlank(message = "hey")
    private String semester;
}

Controller.java

package com.example.demo;

import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@Validated
@RestController("/")
public class Controller {

    
    @PostMapping("student")
    public String getstudent(@Valid @RequestBody student stu,BindingResult result )
    {
    
        System.out.println(result);
        
        return "hello";
    }
}

I am testing it using postman,My request body:

{
    "name":"himu",
    "semester":"7"
}


{
    "name":"",
    "semester":"7"
}

In every request,even if name is empty Its returning 0 errors.

org.springframework.validation.BeanPropertyBindingResult: 0 errors

Can someone help? Thanks in advance.

Advertisement

Answer

you need to add starter validation dependency. Then spring boot auto-configuration will pick the validation related configuration from classpath.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement