gpt4 book ai didi

java - 带有 Jersey : NoClassDefFoundError 的 json

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:56:01 25 4
gpt4 key购买 nike

我正在尝试使用 JAX-RS( Jersey )构建一个 JSON RESTful Web 服务。我也在使用 Maven 构建应用程序。

我的第一个方法是

@Path("/Person")
public class PersonService {
@GET
public String getPersonService() {
Personperson= new Person();
person.setLocationCode("MEL");
person.setName("Johannes");
return person.getName();
}
}

在 pom.xml->clean install + Run On Server 之后它可以工作并且输出与执行的一样。但我想使用 JAXB 获取 JSON 格式的 Person 及其所有属性:

@Path("/Person")
public class PersonService {
@GET
public Person getPersonService() {
Personperson= new Person();
person.setLocationCode("MEL");
person.setName("Johannes");
return person;
}
}

所以我加了

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>

到我的pom文件。额外的 lib 文件(例如 jackson-cors-asl-1.7.1.jar、jackson-jaxrs-1.7.1.jar、jaxb-impl、...)也可在生成的 war 文件中找到。//Edit: 它还下载 stax-api-1.0.1.jar 和 stax-api-1.0-2.jar 这两个文件之一似乎让错误发生

错误:但是调用/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)

根本原因:

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)

当前的解决方法:我没有添加 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>

但现在我必须手动添加 jersey-json.jar。

有没有办法两个添加 jersey-json 两个依赖项,但避免它下载 stax-api.jar 的 ??

最佳答案

我认为 NoClassDefFoundError 是由于您的类路径中可能存在冲突的 JAXB-impl jar 引起的。

如果您认为 Jackson 和与 Jersey 捆绑在一起的 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>

关于java - 带有 Jersey : NoClassDefFoundError 的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14578912/

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