[英]Pattern annotation not working on Business Model in Java
I have a model - 我有一个模特-
package com.somecompany.gis.businessmodel;
import javax.validation.constraints.Pattern;
public class SearchParameters {
//Check if a valid URL
@Pattern(regexp = "(https?:\\/\\/(?:www\\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|www\\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|https?:\\/\\/(?:www\\.|(?!www))[a-zA-Z0-9]\\.[^\\s]{2,}|www\\.[a-zA-Z0-9]\\.[^\\s]{2,})")
String endPoint;
}
I have a controller endpoint - 我有一个控制器端点-
@RequestMapping(value="/search",method=RequestMethod.POST)
public Map<String,Object> search(@Valid @RequestBody SearchParameters searchParameters){
//Do something
}
I was expecting @Pattern
to kick in and give error on Validation error by Jackson. 我期望
@Pattern
参与进来并给出Jackson的Validation错误的错误。 But that is not happening. 但这没有发生。 The regex accepts only valid URLs.
正则表达式仅接受有效的URL。 What am I doing wrong ?
我究竟做错了什么 ?
Dependencies included - 依赖关系包括-
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser</artifactId>
<version>1.0.35</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies>
These are my dependencies in project 这些是我在项目中的依赖
Please check it in this way: 请按以下方式检查它:
@RequestMapping(value="/search",method=RequestMethod.POST)
public Map<String,Object> search(@Valid @RequestBody SearchParameters searchParameters, BindingResult bindingResult){
if (bindingResult.hasErrors()) {
return "errorPage";
}
//Do something
}
bindingResult object you can use for and retrieve validation errors. 您可以用于和检索验证错误的bindingResult对象。
Look at this URL: https://spring.io/guides/gs/validating-form-input/ 查看以下URL: https : //spring.io/guides/gs/validating-form-input/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.