我正在开发一个基于Spring的应用程序并使用泽西框架。 我是这些技术的新手。 我尝试使用默认验证选项(例如:@valid,@ size) 但它不适合我。即使它不会返回异常,有效负载也不会得到验证。
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>JAXServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test.internal</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAXServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
的pom.xml
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.21</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
界面图层
public interface EmplyeeService {
public Response getRoleDetails(@NotNull(message = "name cannot be null")
String empName,
@Size(max = 8) @QueryParam("empDomain")
String empDomain);
}
实施层
@Consumes("application/json")
@Produces("application/json")
@Path("/employee")
public class EmplyeeImpl implements EmplyeeService {
@Override
@GET
@Path("{name}")
public Response getRoleDetails(@NotNull(message = "name cannot be null")
String empName,
@Size(max = 8) @QueryParam("empDomain")
String empDomain) {
return Response.ok().entity().build();
}
的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.test"/>
</beans>
答案 0 :(得分:0)
如果您的Web服务是作为Spring Web服务编写的,那么您需要创建一个Model类:
public class User {
@NotNull
@Size(min = 6, max = 15)
private String name;
@NotNull
@Size(min = 6, max = 15)
private String password;
// getters and setters
}
您的控制器/服务类中的注释应如下所示:
public Response getRoleDetails(@Valid User user, BindingResult result) {
// login logic here
}
BindingResult对象将为您提供表单数据中的验证错误。 Check this example
如果泽西岛网络服务检查Bean Validation Support