我已经使用 Spring-MVC 创建了 RESTful 服务 (POST),并且可以使用 POSTMAN 成功访问它。现在,我尝试对输入 json 进行验证,并且我想使用 Hibernate 验证器并使用其 XML 配置。但验证并未触发。
这是我的服务签名,如下所示:
@RequestMapping(value = "/checkmycar", method = RequestMethod.POST, consumes = "application/json")
public String check(@Valid @RequestBody Car car, BindingResult result) {
if (result.hasErrors()) {
return "ERROR";
}
else
return car.getManufacturer();
}
Run Code Online (Sandbox Code Playgroud)
以下是我的 POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test.hibernate-validator</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
我指的是这份文件。并创建了验证 xml
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd" …Run Code Online (Sandbox Code Playgroud)