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>