[英]json with Jersey: NoClassDefFoundError
I am trying to build a JSON RESTful Webservice with JAX-RS (Jersey). 我正在尝试使用JAX-RS(Jersey)构建JSON RESTful Web服务。 I'm also using Maven to build the App. 我也在使用Maven来构建App。
My first approach was 我的第一个方法是
@Path("/Person")
public class PersonService {
@GET
public String getPersonService() {
Personperson= new Person();
person.setLocationCode("MEL");
person.setName("Johannes");
return person.getName();
}
}
After pom.xml->clean install + Run On Server it works and the output is as execpted. 在pom.xml-> clean install + Run On Server之后它工作,输出就像execpted一样。 But I would like to use JAXB to get the Person in JSON Format with all it's attributes: 但我想使用JAXB来获取JSON格式的Person及其所有属性:
@Path("/Person")
public class PersonService {
@GET
public Person getPersonService() {
Personperson= new Person();
person.setLocationCode("MEL");
person.setName("Johannes");
return person;
}
}
So I added 所以我补充道
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
to my pom file. 到我的pom文件。 The additional lib files (eg jackson-cors-asl-1.7.1.jar, jackson-jaxrs-1.7.1.jar, jaxb-impl,... ) are also available in the generated war file. 其他lib文件(例如jackson-cors-asl-1.7.1.jar,jackson-jaxrs-1.7.1.jar,jaxb-impl,...)也可以在生成的war文件中找到。 //Edit: It also downloads stax-api-1.0.1.jar and stax-api-1.0-2.jar One of theese two files seem to let the error occur //编辑:它还下载了stax-api-1.0.1.jar和stax-api-1.0-2.jar这两个文件之一似乎让错误发生
Error: But calling /Person (or any other Service) leads to an Error: 错误:但是调用/ Person(或任何其他服务)会导致错误:
javax.servlet.ServletException: Servlet.init() for servlet CloudAPIService threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
com.sap.security.auth.service.webcontainer.internal.Authenticator.invoke(Authenticator.java:147)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
com.sap.core.tenant.valve.TenantValidationValve.invokeNextValve(TenantValidationValve.java:169)
com.sap.core.tenant.valve.TenantValidationValve.invoke(TenantValidationValve.java:84)
com.sap.core.js.monitoring.tomcat.valve.RequestTracingValve.invoke(RequestTracingValve.java:27)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)
root cause: 根本原因:
java.lang.NoClassDefFoundError: Could not initialize class com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl
com.sun.xml.bind.v2.model.impl.RuntimeTypeInfoSetImpl.<init>(RuntimeTypeInfoSetImpl.java:61)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:129)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:81)
com.sun.xml.bind.v2.model.impl.ModelBuilder.<init>(ModelBuilder.java:152)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.<init>(RuntimeModelBuilder.java:89)
com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:456)
com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:302)
com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1136)
com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)
com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:121)
com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:202)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:133)
javax.xml.bind.ContextFinder.find(ContextFinder.java:286)
javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:244)
com.sun.jersey.server.impl.wadl.WadlApplicationContextImpl.<init>(WadlApplicationContextImpl.java:72)
com.sun.jersey.server.impl.wadl.WadlFactory.init(WadlFactory.java:97)
com.sun.jersey.server.impl.application.RootResourceUriRules.initWadl(RootResourceUriRules.java:169)
com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:106)
com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1298)
com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:169)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:775)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:771)
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
javax.servlet.GenericServlet.init(GenericServlet.java:244)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
com.sap.security.auth.service.webcontainer.internal.Authenticator.invoke(Authenticator.java:147)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
com.sap.core.tenant.valve.TenantValidationValve.invokeNextValve(TenantValidationValve.java:169)
com.sap.core.tenant.valve.TenantValidationValve.invoke(TenantValidationValve.java:84)
com.sap.core.js.monitoring.tomcat.valve.RequestTracingValve.invoke(RequestTracingValve.java:27)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)
Current workaround: Instead of adding the jersey-json dependency, I added jackson dependencies directly: 当前的解决方法:我没有添加jersey-json依赖项,而是直接添加了jackson依赖项:
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.11</version>
</dependency>
But now I have to add the jersey-json.jar manually. 但现在我必须手动添加jersey-json.jar。
Is there a way two add jersey-json two depenedencies, but avoiding that it downloads the stax-api.jar's ?? 有没有办法两个添加jersey-json两个依赖项,但避免它下载stax-api.jar的?
I believe the NoClassDefFoundError is caused because of a conflicting JAXB-impl jar which may be present in your classpath. 我相信NoClassDefFoundError是由于一个冲突的JAXB-impl jar引起的,它可能存在于你的类路径中。
If you think, Jackson and stax-api dependencies bundled with Jersey are causing this problem (I suspect that is not the case), you can exclude them from jersey-json dependency in your maven pom. 如果你认为,与泽西捆绑在一起的杰克逊和stax-api依赖会导致这个问题(我怀疑情况并非如此),你可以将它们排除在你的maven pom中的jersey-json依赖之外。
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
<exclusions>
<exclusion>
<artifactId>jettison</artifactId>
<groupId>org.codehaus.jettison</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-core-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-mapper-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-jaxrs</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-xc</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
<exclusion>
<artifactId>stax-api</artifactId>
<groupId>javax.xml.stream</groupId>
</exclusion>
</exclusions>
</dependency>
Possible solution (worked for me): 可能的解决方案(为我工作):
It turned out that stax comes with Java 1.6 . 事实证明, stax附带Java 1.6 。 But jersey downloaded the two mentioned stax jar files. 但是泽西下载了两个提到的stax jar文件。 So I guess the error occured because there are duplicate classes. 所以我猜错误是因为有重复的类。
I just excluded the corresponding package in the pom.xml and it works now: 我刚刚在pom.xml中排除了相应的包,它现在可以正常工作:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.14</version>
<exclusions>
<exclusion>
<artifactId>stax-api</artifactId>
<groupId>stax</groupId>
</exclusion>
<exclusion>
<artifactId>stax</artifactId>
<groupId>stax</groupId>
</exclusion>
</exclusions>
</dependency>
I know the question is quite old, but maybe the answer might be usefull for the future generations ;) I solved the problem using 我知道问题已经很老了,但也许答案可能对后代有用;)我用问题解决了问题
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.0.1</version>
</dependency>
As far as I know, JAXB is for XML Binding. 据我所知,JAXB用于XML绑定。 If you want JSON you may have a look at http://wiki.fasterxml.com/JacksonJAXBAnnotations 如果你想要JSON,你可以查看http://wiki.fasterxml.com/JacksonJAXBAnnotations
this could be of interest: How to reuse Jersey's JSON/JAXB for serialization? 这可能是有趣的: 如何重用Jersey的JSON / JAXB进行序列化?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.