javaspring-bootjavax.validation

javax.validation 2.0.1 List<Object> not working in spring boot


I am new the spring boot developement. I am trying to validate the post request by passing List from @RequestBody. Below is control class

@CrossOrigin
@RestController
@RequestMapping("/webapi/device")
@Validated
public class DeviceController extends AuthControllerImpl{

    @Autowired
    private DeviceServices deviceServices;


    //Test Postman request 01
    @PostMapping(path = "/udateDevices", consumes = MediaType.APPLICATION_JSON,  produces = MediaType.APPLICATION_JSON)
    public ResponseEntity<Object> updateDeviceToDB( @RequestBody List<@Valid Device> device, @RequestParam("token") String token, Errors errors)    {
        if (errors.hasErrors()) {
            return new ResponseEntity<Object>(new ErrorResponse(errors), HttpStatus.BAD_REQUEST);
        }

        if(isValidToken(token) != null){
            DeviceControllerResponse response = deviceServices.updateDeviceToDB(device);
            if (!response.isSuccess()) {
                return new ResponseEntity<Object>(response, HttpStatus.BAD_REQUEST);
            }
            return ResponseEntity.ok(response); 
        }else {
            return new ResponseEntity<Object>("Token has been expired/not valid.", HttpStatus.UNAUTHORIZED);
        }

    }
}

Below is my entity class.

import javax.validation.constraints.NotEmpty;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "rpDevices")
public class Device {

    @Id
    private String id;

    @NotEmpty(message = "device udid should not be empty")
    private String udid;

    @NotEmpty(message = "deviceModel should not be empty")
    private String deviceModel;

    @NotEmpty(message = "device location should not be empty")
    private String location;

    @NotEmpty(message = "device port should not be empty")
    private String port;

    private String url;

    private String lastUpdate;
    private String imsi;
    private String msisdn;

    private String aliasName;

    public Device() {
        super();
    }

    public Device(String id, String udid, String deviceModel, String location, String port, String url,
            String lastUpdate, String imsi, String msisdn, String aliasName) { 
        this.id = id;
        this.udid = udid;
        this.deviceModel = deviceModel;
        this.location = location;
        this.port = port;
        this.url = url;
        this.lastUpdate = lastUpdate;
        this.imsi = imsi;
        this.msisdn = msisdn;
        this.aliasName = aliasName;
    }
    //Getter and setters 

}

It never validates the entity and giving the below error.

    {
    "timestamp": 1591497348682,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "javax.validation.UnexpectedTypeException",
    "message": "HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.String'. Check configuration for 'updateDeviceToDB.device[0].port'",
    "path": "/xxxx/webapi/device/udateDevices"
}

Can some one help how to validate the List directly from request boday. https://www.baeldung.com/spring-validate-list-controller I tried this but not helps.

here are the pom dependencies

        <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>1.5.21.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>


        <!-- Adding spring boot cap -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Adding spring boot security,ldap -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ldap</groupId>
            <artifactId>spring-ldap-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        <!-- starter-data-mongodb MongoRepository -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

        <!-- javax.mail -->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.5</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>

        <!-- some other stuff related to testing -- >

</dependencies>

Solution

  • validation-api is the specification, hibernate-validator is the implementation, simply speaking.

    spring-boot-starter-web will automatically import hibernate-validator, then hibernate-validator will automatically import validation-api.

    With your jar dependencies configuration, there are two version of validation-api. One is 1.1.0 imported by hibernate-validator 5.3.6 (from spring-boot-starter-web), and the other is 2.0.1 imported by your explicit declaration. validation-api 2.0.1 will be used with hibernate-validator 5.3.6, which is incompatible. Causing no validator could be found error.

    When you remove validation-api 2.0.1, 1.1.0 version will be used. But javax.validation.constraints.NotEmpty not introduced with validation-api 1.1.0, so you will get another error.

    Several ways to solve this problem:

    1. remove validation-api 2.0.1, use org.hibernate.validator.constraints.NotEmpty instead of javax.validation.constraints.NotEmpty.
    2. remove validation-api 2.0.1, upgrade spring boot version to 2.x, it will use hibernate-validator 6.x and validation-api 2.x.
    3. keep your explicit validation-api 2.0.1 declaration. Then add another hibernate-validator 6.0.14 declaration. This case should be fully tested, cause there are maybe some Spring support issues.