[英]javax.validation 2.0.1 List<Object> not working in spring boot
I am new the spring boot developement.我是新的 spring 引导开发。 I am trying to validate the post request by passing List from @RequestBody.我正在尝试通过从@RequestBody 传递列表来验证发布请求。 Below is control class下面是控制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.下面是我的实体 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. https://www.baeldung.com/spring-validate-list-controller我试过这个但没有帮助。
here are the pom dependencies这是 pom 依赖项
<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>
validation-api is the specification, hibernate-validator is the implementation, simply speaking. validation-api是规范, hibernate-validator是实现,简单来说。
spring-boot-starter-web will automatically import hibernate-validator , then hibernate-validator will automatically import validation-api . spring-boot-starter-web会自动导入hibernate-validator ,然后hibernate-validator会自动导入validation-api 。
With your jar dependencies configuration, there are two version of validation-api
.使用您的 jar 依赖项配置,有两个版本的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.一个是由hibernate-validator 5.3.6
(来自 spring-boot-starter-web)导入的1.1.0
,另一个是由您的显式声明导入的2.0.1
。 validation-api 2.0.1
will be used with hibernate-validator 5.3.6
, which is incompatible. validation-api 2.0.1
将与不兼容的hibernate-validator 5.3.6
一起使用。 Causing no validator could be found error.导致找不到验证器错误。
When you remove validation-api 2.0.1
, 1.1.0
version will be used.当您删除validation-api 2.0.1
时,将使用1.1.0
版本。 But javax.validation.constraints.NotEmpty
not introduced with validation-api 1.1.0
, so you will get another error.但是javax.validation.constraints.NotEmpty
没有在validation-api 1.1.0
中引入,所以你会得到另一个错误。
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
,使用org.hibernate.validator.constraints.NotEmpty
而不是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
,将 spring 引导版本升级到 2.x,它将使用 hibernate-validator 6.x 和 validation-api 2.x。validation-api 2.0.1
declaration.保留您的显式validation-api 2.0.1
声明。 Then add another hibernate-validator 6.0.14
declaration.然后添加另一个hibernate-validator 6.0.14
声明。 This case should be fully tested, cause there are maybe some Spring support issues.这种情况应该经过全面测试,因为可能存在一些 Spring 支持问题。I was having the similar issue, if you are using spring boot version (2.3.0) and above then validation-api
wont work.我遇到了类似的问题,如果您使用的是 spring 启动版本(2.3.0)及更高版本,那么validation-api
将无法工作。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Just add the above dependency in pom.xml
.只需在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请参阅发行说明: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#validation-starter-no-longer-included-in-web-starters
The Errors
parameter should follow the validated parameter: Errors
参数应遵循已验证的参数:
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.为了在单一方法中支持多个经过验证的参数,这是必需的。
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.在将用户输入绑定到 model 后验证它是很常见的。 Spring 3 provides support for declarative validation with JSR-303. Spring 3 支持使用 JSR-303 进行声明式验证。 This support is enabled automatically if a JSR-303 provider, such as Hibernate Validator, is present on your classpath.如果您的类路径中存在 JSR-303 提供程序(例如 Hibernate Validator),则会自动启用此支持。 When enabled, you can trigger validation simply by annotating a Controller method parameter with the @Valid annotation:启用后,您只需使用 @Valid 注释注释 Controller 方法参数即可触发验证:
Visit This Link: https://spring.io/blog/2009/11/17/spring-3-type-conversion-and-validation/访问此链接: https://spring.io/blog/2009/11/17/spring-3-type-conversion-and-validation/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.