无法在Tomcat容器上使用Jersey和Hibernate Validator设置项目

时间:2018-08-01 19:21:37

标签: tomcat jax-rs jersey-2.0 hibernate-validator weld

我在Tomcat 8.0中有一个使用Jersey 2.27,Weld 3.0.4.Final,JJWT的JAX-RS项目,可以正常工作。现在,我想使用Hibernate Validator进行一些验证。但是,当我在pom.xml重新启动tomcat上添加Jersey验证器时,出现以下异常:

  

GRAVE:StandardWrapper.Throwable java.lang.IllegalArgumentException:无法将javax.validation.ValidatorFactory字段org.glassfish.jersey.server.validation.internal.ValidationBinder $ ConfiguredValidatorProvider.factory设置为org.hibernate.validator.internal.engine java.lang.reflect.Field.set(UnknownObject)上sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(未知源)的.ConfigurationImpl源代码)org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:94)在org.jboss.weld.util.Beans.injectBoundFields(Beans.java:336)在org.jboss.weld.util.Beans org.jboss.weld.injection.producer.ResourceInjector $ 1.proceed(ResourceInjector.java:69)的.injectFieldsAndInitializers(Beans.java:347)在org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)在org.jboss.weld.injection.produ cer.ResourceInjector.inject(ResourceInjector.java:71)位于org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:117)位于org.glassfish.jersey.inject.cdi.se.injector.JerseyInjectionTarget。在org.glassfish.jersey.inject.cdi.se.bean.SupplierClassBean处注入(JerseyInjectionTarget.java:171)在org.glassfish.jersey.inject.cdi.se.bean.SupplierClassBean.create(SupplierClassBean.java:127)在org.glassfish.jersey.inject.cdi.se.bean.SupplierClassBean。在org.org.jboss.weld.contexts.AbstractContext.get(AbstractContext.java:96)处org.jboss.weld.bean.ContextualInstanceStrategy $ DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)处创建(SupplierClassBean.java:89) org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:700)上的.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)在org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl .java:723),位于org.jboss.weld.bean.builti上org.jboss.weld.util.ForwardingBeanManager.getReference(ForwardingBeanManager.java:64) n.BeanManagerProxy.getReference(BeanManagerProxy.java:86)在org.glassfish.jersey.inject.cdi.se.bean.SupplierBeanBridge.getSupplier(SupplierBeanBridge.java:162)在org.glassfish.jersey.inject.cdi.se。位于org.jboss.weld.bean.ContextualInstanceStrategy $ DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy。 org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)处org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:700)处org.jboss.weld.manager处org.jboss.weld.util.ForwardingBeanManager.getReference(ForwardingBeanManager.java:64)处的.BeanManagerImpl.getReference(BeanManagerImpl.java:723)在org.jboss.weld.bean.builtin.BeanManagerProxy.getReference(BeanManagerProxy.java:86) ),网址为org.glassfish.jersey.inject.cdi.se.CdiSeInjectionManager.getInstanceInternal(CdiSeInjectionManager.java:183) org.glassfish.jersey.server.model.internal.ResourceMethodInvokerConfigurator.lambda $ postInit $ 0(ResourceMethodInvokerConfigurator.java:78)上的org.glassfish.jersey.inject.cdi.se.CdiSeInjectionManager.getInstance(CdiSeInjectionManager.java:166) org.glassfish.jersey.server.internal.routing.RuntimeModelBuilder.createInflector(RuntimeModelBuilder.java:130)上的.glassfish.jersey.server.model.ResourceMethodInvoker $ Builder.build(ResourceMethodInvoker.java:218)在org.glassfish.jersey上org.glassfish.jersey.server.internal.routing.RuntimeModelBuilder.createResourceMethodRouters(RuntimeModelBuilder.java:311)位于org.glassfish.jersey.server.internal的.server.internal.routing.RuntimeModelBuilder.createMethodRouter(RuntimeModelBuilder.java:117) org.glassfish.jersey.server.internal.routing.Routing $ Builder.buildStage(Routing.java:247)上的.routing.RuntimeModelBuilder.buildModel(RuntimeModelBuilder.java:175)在org.glassfish.jersey.server.ApplicationHandler.initialize (ApplicationHandler.java:422)在org.glassfish.jersey.internal.Errors.process(Errors.java:316)上的org.glassfish.jersey.jersey.server.ApplicationHandler.lambda $ initialize $ 1(ApplicationHandler.java:316) org.glassfish.jersey.internal.Errors.processWithException(Errors.java:256)的org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:315)的.process(Errors.java:298)。 org.glassfish.jersey.servlet.WebComponent。(WebComponent.java:335)上的glassfish.jersey.server.ApplicationHandler。(ApplicationHandler.java:282)org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java: 178),位于org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper)的org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370),位于javax.servlet.GenericServlet.init(GenericServlet.java:158)处org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1034)的.java:1231)org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext。 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5270)处org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)处org.apache.catalina.core处的Java:4978) java的org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1398)的.ContainerBase $ StartChild.call(ContainerBase.java:1408)在Java的java.util.concurrent.FutureTask.run(未知源) .util.concurrent.ThreadPoolExecutor.runWorker(未知源)在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知源)在java.lang.Thread.run(未知源)

我已经阅读了有关Jersey和Bean验证Jersey Bean Validation的文档。

按照配置步骤,我创建了一个实现ContextResolver的Java类ValidationConfigurationContextResolver:

public class ValidationConfigurationContextResolver implements ContextResolver<ValidationConfig> {

    @Context
    private ResourceContext resourceContext;

    @Override
    public ValidationConfig getContext(final Class<?> type) {
        final ValidationConfig config = new ValidationConfig();
        config.constraintValidatorFactory(resourceContext.getResource(InjectingConstraintValidatorFactory.class));
        config.parameterNameProvider(new CustomParameterNameProvider());
        return config;
    }

    /**
     * See ContactCardTest#testAddInvalidContact.
     */
    private class CustomParameterNameProvider implements ParameterNameProvider {

        private final ParameterNameProvider nameProvider;

        public CustomParameterNameProvider() {
            nameProvider = Validation.byDefaultProvider().configure().getDefaultParameterNameProvider();
        }

        @Override
        public List<String> getParameterNames(final Constructor<?> constructor) {
            return nameProvider.getParameterNames(constructor);
        }

        @Override
        public List<String> getParameterNames(final Method method) {
            // See ContactCardTest#testAddInvalidContact.
            if ("addContact".equals(method.getName())) {
                return Arrays.asList("contact");
            }
            return nameProvider.getParameterNames(method);
        }
    }
}

接下来,我创建了ResourceConfig的子类并注册了ValidationConfigurationContextResolver:

@ApplicationPath("/api")
public class CoreVendasGSJSApplication extends ResourceConfig {
    public CoreVendasGSJSApplication() {
        register(ValidationConfigurationContextResolver.class);
    }
}

但是,当我重新启动Tomcat时,又遇到了另一个异常

    GRAVE: Servlet [br.com.gruposaojosesaude.corevendas.app.CoreVendasGSJSApplication] in web application [/CoreVendasGSJS] threw load() exception
org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type Configuration with qualifiers @Default
  at injection point [BackedAnnotatedParameter] Parameter 2 of [BackedAnnotatedConstructor] @Inject public org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl(InjectionManager, Configuration, ExtendedResourceContext)
  at org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl.<init>(WadlApplicationContextImpl.java:0)
  Possible dependencies: 
  - org.glassfish.jersey.inject.cdi.se.bean.InstanceBean@e310c41,
  - Managed Bean [class br.com.gruposaojosesaude.corevendas.app.CoreVendasGSJSApplication] with qualifiers [@Any @Default]

我知道这是由于CDI不知道应该注入哪个bean,但是当涉及Jersey + CDI(Weld)+ Hibernate Validator时,我不知道如何解决该问题。我以前的研究找不到任何东西。

有人已经有类似的东西了吗?

谢谢。

0 个答案:

没有答案