Reputation: 584
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>
Upvotes: 2
Views: 6227
Reputation: 71
public ResponseEntity<Object> updateDeviceToDB(@Valid @RequestBody List<Device> device, @RequestParam("token") String token, Errors errors)
It is common to validate a model after binding user input to it. Spring 3 provides support for declarative validation with JSR-303. This support is enabled automatically if a JSR-303 provider, such as Hibernate Validator, is present on your classpath. When enabled, you can trigger validation simply by annotating a Controller method parameter with the @Valid annotation:
Visit This Link : https://spring.io/blog/2009/11/17/spring-3-type-conversion-and-validation/
Upvotes: 0
Reputation: 338
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:
validation-api 2.0.1
, use org.hibernate.validator.constraints.NotEmpty
instead of javax.validation.constraints.NotEmpty
.validation-api 2.0.1
, upgrade spring boot version to 2.x, it will use hibernate-validator 6.x and validation-api 2.x.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.Upvotes: 6
Reputation: 121
I was having the similar issue, if you are using spring boot version (2.3.0) and above then validation-api
wont work.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Just add the above dependency in pom.xml
.
See release notes: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#validation-starter-no-longer-included-in-web-starters
Upvotes: 4
Reputation: 1648
The Errors
parameter should follow the validated parameter:
public ResponseEntity<Object> updateDeviceToDB( @RequestBody List<@Valid Device> device, Errors errors, @RequestParam("token") String token)
This is required in order to support multiple validated parameters in a single method.
Upvotes: 0