线程“main”中的exceptionjava.lang.NoClassDefFoundError:无法初始化类com.sun.jersey.core.header.MediaTypes

我正试图经营一个jersey客户并面对这个问题。

WS类:

import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @Path("/hello") public class HelloWorldService { @GET @Path("/vip") @Produces(MediaType.APPLICATION_JSON) public Response getMsg(@QueryParam("msg") String msg) { String output = "Jersey say : " + msg; return Response.status(200).entity(output).build(); } } 

客户类:

  import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; public class JerseyClientGet { public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/TestRest/rest/hello/vip?msg=ABCD"); ClientResponse response = webResource.accept("application/json") .get(ClientResponse.class); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } String output = response.getEntity(String.class); System.out.println("Output from Server .... \n"); System.out.println(output); } catch (Exception e) { e.printStackTrace(); } } } 

问题是当我尝试执行客户端类时,会出现以下错误消息

  Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.sun.jersey.core.header.MediaTypes at com.sun.jersey.core.spi.factory.MessageBodyFactory.initReaders(MessageBodyFactory.java:182) at com.sun.jersey.core.spi.factory.MessageBodyFactory.initReaders(MessageBodyFactory.java:176) at com.sun.jersey.core.spi.factory.MessageBodyFactory.init(MessageBodyFactory.java:162) at com.sun.jersey.api.client.Client.init(Client.java:342) at com.sun.jersey.api.client.Client.access$000(Client.java:118) at com.sun.jersey.api.client.Client$1.f(Client.java:191) at com.sun.jersey.api.client.Client$1.f(Client.java:187) at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) at com.sun.jersey.api.client.Client.(Client.java:187) at com.sun.jersey.api.client.Client.(Client.java:159) at com.sun.jersey.api.client.Client.create(Client.java:669) at com.mkyong.client.JerseyClientGet.main(JerseyClientGet.java:12) 

我正在使用以下jar子:

asm-3.1.jar,jackson-core-asl-1.9.2.jar,jackson-jaxrs-1.9.2.jar,jackson-mapper-asl-1.9.2.jar,jackson-xc-1.9.2.jar, jersey-bundle-1.8.jar,jersey-client-1.18.jar,jersey-core-1.18.jar,jersey-json-1.18.jar,jersey-server-1.18.jar,jersey-servlet-1.18.jar,jettison- 1.1.jar,jsr311-api-1.1.1.jar

通常,当您的代码针对jersey-bundle-1.8.jar和jsr311-api-0.9.jar进行编译时,您会遇到此问题。 但在这里我可以看到你正在使用jsr311-api-1.1.1.jar 。 然后下一个问题是应用程序/ Web服务器将加载旧的jar文件。 例如:GlassFish 3.1一个带有Jersy 1.5(可能优先于你的库)。

理想情况下,您需要检查服务器中加载的JSR-311库的版本(jsr311-api jar的0.9版本已过时)。 你应该针对jersey-bundle-1.8.jar进行编译,并使用jersey-bundle-1.8.jar和jsr311-api-1.1.1.jar运行

在我的情况下,我并没有将jersey-core jar作为运行时依赖项。 一旦我添加它似乎工作正常。

对于粉丝,这些(在这种情况下使用mockito,但有点通用):

 java.lang.NoClassDefFoundError: com/sun/jersey/spi/inject/Errors$Closure java.lang.NoClassDefFoundError: com/sun/jersey/core/header/LinkHeaders 

意味着“你依赖于jersey-core 1.0.2以及对jersey-client 1.11的依赖”(需要更紧密地匹配核心与客户端版本)。 不幸的是,“服务器”和“客户端”都使用“核心”,所以最后它们几乎都必须精确匹配:|