gpt4 book ai didi

java - Jersey 2 bean 验证不起作用

转载 作者:行者123 更新时间:2023-11-30 08:52:55 25 4
gpt4 key购买 nike

我现在使用的是 jersey2.13 和 spring4.X,无法让 jersey bean 验证工作。

以下是我的配置和代码片段:

网络.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();
}
}

人.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._%-]+@[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 + "]";
}
}

最佳答案

不确定为什么要禁用所有内容,但在测试后,删除禁用属性使其工作

//property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
//property(ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);
//property(ServerProperties.BV_FEATURE_DISABLE, true);

这些属性默认为 false,允许自动发现。您正在禁用它。

关于java - Jersey 2 bean 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com