我正在尝试运行 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.<init>(Client.java:187)
at com.sun.jersey.api.client.Client.<init>(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 核心-asl-1.9.2.jar, jackson -jaxrs-1.9.2.jar, jackson 映射器-asl-1.9.2.jar, jackson -xc-1.9.2.jar, 泽西-bundle-1.8.jar, Jersey 客户端1.18.jar, Jersey 核心 1.18.jar, Jersey -json-1.18.jar, Jersey 服务器-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
。那么下一个问题就是较旧的 jar 文件会被应用程序/Web 服务器加载。例如:GlassFish 3.1 带有 Jersy 1.5(它可能优先于您的库)。
理想情况下,您需要检查服务器中是否加载了 JSR-311 库的版本(0.9 版的 jsr311-api jar 已过时)。 你应该针对 jersey-bundle-1.8.jar 进行编译,并使用 jersey-bundle-1.8.jar 和 jsr311-api-1.1.1.jar 运行
关于java - 线程 "main"java.lang.NoClassDefFoundError : Could not initialize class com. sun.jersey.core.header.MediaTypes 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20487783/