我有一个模特-
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;
}
我有一个控制器端点-
@RequestMapping(value="/search",method=RequestMethod.POST)
public Map<String,Object> search(@Valid @RequestBody SearchParameters searchParameters){
//Do something
}
我期望@Pattern
介入,并为Jackson的Validation错误提供错误信息。但这没有发生。正则表达式仅接受有效的URL。我在做什么错了?
包含依赖项-
<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>
这些是我在项目中的依赖项
答案 0 :(得分:0)
请通过以下方式进行检查:
@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对象,可用于和检索验证错误。