1
我現在用的就是jersey2.13和spring4.X,並不能得到球衣Bean驗證工作。球衣2 Bean驗證不要工作
以下是我的配置和代碼段:
的web.xml
<servlet>
<!-- jersey ServletContainer -->
<servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- jersey resource packages-->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.wordnik.swagger.jaxrs.json,
com.ghca.easyview.server.api.resource
</param-value>
</init-param>
<!-- jersey provider classnames-->
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
com.wordnik.swagger.jersey.listing.ApiListingResourceJSON,
com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider,
com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.wadl.disableWadl</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.ghca.easyview.server.api.context.JerseyApplication</param-value>
</init-param>
<!-- jersey beanValidation -->
<init-param>
<param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/se/rest/*</url-pattern>
</servlet-mapping>
<!-- jersey swagger UI -->
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://10.143.132.99:4000/api/se/rest</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Bootstrap</servlet-name>
<servlet-class>com.ghca.easyview.server.api.plugin.swagger.Bootstrap2</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
JerseyApplication.java:
包com.ghca.easyview.server.api.context;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.ContextResolver;
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.validation.ValidationConfig;
import org.glassfish.jersey.server.validation.internal.InjectingConstraintValidatorFactory;
/**
* ContactCard application configuration
* JAX-RS application
*/
public class JerseyApplication extends ResourceConfig{
public JerseyApplication(){
property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
property(ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
property(ServerProperties.BV_FEATURE_DISABLE, true);
// Resource Package Address
packages("com.ghca.easyview.server.api.resource");
//Validation.
register(ValidationConfigurationContextResolver.class);
}
/**
*/
public static class ValidationConfigurationContextResolver implements ContextResolver<ValidationConfig>{
@Context
private ResourceContext resourceContext;
@Override
public ValidationConfig getContext(final Class<?> type) {
return new ValidationConfig().constraintValidatorFactory(resourceContext.getResource(InjectingConstraintValidatorFactory.class));
}
}
}
PersonResource.java:
package com.ghca.easyview.server.api.resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.springframework.stereotype.Component;
import com.ghca.easyview.server.api.request.validation.model.Person;
import com.wordnik.swagger.annotations.Api;
@Component
@Path("/person")
@Api(value = "/person", description = "person", position = 1)
@Produces({ "application/json", "application/xml" })
public class PersonResource {
@POST
public Response create(@Valid @NotNull Person person) {
System.out.println(person);
return Response.ok().build();
}
}
Person.java:
package com.ghca.easyview.server.api.request.validation.model;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
public class Person {
@NotNull
private Integer id;
@NotNull
@Length(min = 2, max = 20)
private String name;
@Email(message = "{contact.wrong.email}", regexp = "[a-zA-Z0-9._%-][email protected][a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}")
private String email;
@Pattern(message = "{contact.wrong.phone}", regexp = "[0-9]{3,9}")
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", email=" + email
+ ", phone=" + phone + "]";
}
}
嗨,peeskillet,還是不是?我把它評論 – jack
@jack吧??? ... –
不工作,消除禁用 – jack