我正在尝试运行java rest webservice程序。我经常收到这个错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/hk2/api/ServiceLocatorFactory$CreatePolicy
at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:141)
at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:113)
at org.glassfish.jersey.server.internal.RuntimeDelegateImpl.<init>(RuntimeDelegateImpl.java:63)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:117)
at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:165)
at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:135)
at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)
at javax.ws.rs.core.UriBuilder.newInstance(UriBuilder.java:95)
at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
at co.test.RestClient.getBaseUri(RestClient.java:31)
at co.test.RestClient.main(RestClient.java:18)
Caused by: java.lang.ClassNotFoundException: org.glassfish.hk2.api.ServiceLocatorFactory$CreatePolicy
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 16 more
java类
Hello.java
package co.test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayStringHello(){
String Str="this is hello plain";
return Str;
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHTMLHello(){
String Str="<html><title>title</title><body><h1>hello from html</h1></body></html>";
return Str;
}
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello(){
String Str="<?xml version=\"1.0\"><title>title</title><body><h1>hello from html</h1></body>";
return Str;
}
}
RestClient.java
package co.test;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
public class RestClient {
public static void main(String[] args) {
ClientConfig config= new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target= client.target(RestClient.getBaseUri());
String plain=target.path("rest").path("hello").request().accept(MediaType.TEXT_PLAIN).get(String.class);
String html=target.path("rest").path("hello").request().accept(MediaType.TEXT_HTML).get(String.class);
String xml=target.path("rest").path("hello").request().accept(MediaType.TEXT_XML).get(String.class);
System.out.println(plain);
System.out.println(html);
System.out.println(xml);
}
private static URI getBaseUri(){
return UriBuilder.fromUri("http://localhost:8080/RestWSDemo/").build();
}
}
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RestWSDemo</display-name>
<welcome-file-list>
<welcome-file>Index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>JerseyRestService</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>co.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyRestService</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="rest/hello">Hello</a>
</body>
</html>
我正在使用的jar文件是:
任何人都可以帮我这个吗?谢谢