从Spring-Jersey 1.x迁移到2.x.

时间:2014-03-14 23:20:19

标签: java json spring jaxb jersey-2.0

我已经使用Spring 3.1和Spring-Jersey 1.x,Jackson 2.x和Hibernate一段时间了。我想升级到Spring-Jersey 2.6。

根据Jersey用户指南和示例应用,我包含所有相关的依赖项。我有一种感觉,我的问题可能与我使用Jackson和JAXB注释的POJO有关,但我似乎无法弄清楚如何前进。杰克逊以前解析JAX-B注释似乎没有任何问题,所以我不确定如何修改我的配置。

我的应用程序看起来像:

    public class MyApplication extends ResourceConfig {

        public MyApplication(){
            packages(true,"com.my.app");
            register(JacksonContextResolver.class); //Custom Context Resolver
            register(JacksonFeature.class);
        }
    }

启动时我收到此错误:

    MultiException stack 1 of 3
    java.lang.NullPointerException
        at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.setConfiguration(AbstractJAXBProvider.java:109)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.glassfish.hk2.utilities.reflection.ReflectionHelper.invoke(ReflectionHelper.java:1017)
        at org.jvnet.hk2.internal.ClazzCreator.methodMe(ClazzCreator.java:375)
        at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:428)
        at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
        at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:69)
        at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
        at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:98)
        at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87)
        at org.glassfish.jersey.internal.inject.ProviderToService.apply(ProviderToService.java:58)
        at org.glassfish.jersey.internal.inject.ProviderToService.apply(ProviderToService.java:54)
        at jersey.repackaged.com.google.common.collect.Iterators$8.transform(Iterators.java:860)
        at jersey.repackaged.com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
        at java.util.AbstractCollection.addAll(AbstractCollection.java:333)
        at java.util.LinkedHashSet.<init>(LinkedHashSet.java:169)
        at jersey.repackaged.com.google.common.collect.Sets.newLinkedHashSet(Sets.java:292)
        at org.glassfish.jersey.internal.inject.Providers.getClasses(Providers.java:364)
        at org.glassfish.jersey.internal.inject.Providers.getProviders(Providers.java:186)
        at org.glassfish.jersey.message.internal.MessageBodyFactory.<init>(MessageBodyFactory.java:304)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
        at org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1105)
        at org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:292)
        at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:422)
        at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
        at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:114)
        at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:102)
        at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:97)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:154)
        at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:199)
        at org.jvnet.hk2.internal.SingletonContext.findOrCreate(SingletonContext.java:153)
        at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:621)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:606)
        at org.glassfish.jersey.server.model.ComponentModelValidator.<init>(ComponentModelValidator.java:97)
        at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:457)
        at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:162)
        at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:322)
        at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
        at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
        at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:319)
        at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:284)
        at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
        at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:168)
        at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:388)
        at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
        at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4001)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4651)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:905)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:740)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
        at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1345)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:303)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
        at java.lang.Thread.run(Thread.java:722)
    MultiException stack 2 of 3
    java.lang.IllegalStateException: Unable to perform operation: method inject on com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App
        at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:449)
        at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
        at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:69)
        at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
        at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:98)
        at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87)
        at org.glassfish.jersey.internal.inject.ProviderToService.apply(ProviderToService.java:58)
        at org.glassfish.jersey.internal.inject.ProviderToService.apply(ProviderToService.java:54)
        at jersey.repackaged.com.google.common.collect.Iterators$8.transform(Iterators.java:860)
        at jersey.repackaged.com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
        at java.util.AbstractCollection.addAll(AbstractCollection.java:333)
        at java.util.LinkedHashSet.<init>(LinkedHashSet.java:169)
        at jersey.repackaged.com.google.common.collect.Sets.newLinkedHashSet(Sets.java:292)
        at org.glassfish.jersey.internal.inject.Providers.getClasses(Providers.java:364)
        at org.glassfish.jersey.internal.inject.Providers.getProviders(Providers.java:186)
        at org.glassfish.jersey.message.internal.MessageBodyFactory.<init>(MessageBodyFactory.java:304)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
        at org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1105)
        at org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:292)
        at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:422)
        at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
        at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:114)
        at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:102)
        at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:97)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:154)
        at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:199)
        at org.jvnet.hk2.internal.SingletonContext.findOrCreate(SingletonContext.java:153)
        at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:621)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:606)
        at org.glassfish.jersey.server.model.ComponentModelValidator.<init>(ComponentModelValidator.java:97)
        at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:457)
        at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:162)
        at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:322)
        at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
        at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
        at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:319)
        at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:284)
        at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
        at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:168)
        at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:388)
        at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
        at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4001)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4651)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:905)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:740)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
        at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1345)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:303)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
        at java.lang.Thread.run(Thread.java:722)
    MultiException stack 3 of 3
    java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.message.internal.MessageBodyFactory
        at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:443)
        at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
        at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:114)
        at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:102)
        at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:97)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:154)
        at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:199)
        at org.jvnet.hk2.internal.SingletonContext.findOrCreate(SingletonContext.java:153)
        at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:621)
        at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:606)
        at org.glassfish.jersey.server.model.ComponentModelValidator.<init>(ComponentModelValidator.java:97)
        at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:457)
        at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:162)
        at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:322)
        at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
        at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
        at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:319)
        at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:284)
        at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
        at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:168)
        at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:388)
        at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
        at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4001)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4651)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:905)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:740)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
        at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1345)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:303)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
        at java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:14)

当我从Spring-Jersey 1.x升级到2.x时,我遇到了完全相同的问题。

我发现我还在使用jersey-core-1.18.1 jar。

删除它修复了这个问题!