我创建了一个 REST Web 服务。然后尝试使用 GWT 访问它。但我收到以下错误
com.sun.jersey.core.spi.component.ProviderFactory __getComponentProvider
SEVERE: The provider class, class com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General, could not be instantiated. Processing will continue but the class will not be utilized
java.lang.IllegalArgumentException: argument type mismatch
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 com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:127)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:135)
at com.sun.jersey.core.spi.component.ComponentConstructor._getInstance(ComponentConstructor.java:198)
at com.sun.jersey.core.spi.component.ComponentConstructor.getInstance(ComponentConstructor.java:169)
at com.sun.jersey.core.spi.component.ProviderFactory.__getComponentProvider(ProviderFactory.java:166)
at com.sun.jersey.core.spi.component.ProviderFactory.getComponentProvider(ProviderFactory.java:137)
at com.sun.jersey.core.spi.component.ProviderServices.getComponent(ProviderServices.java:256)
at com.sun.jersey.core.spi.component.ProviderServices.getServices(ProviderServices.java:160)
at com.sun.jersey.core.spi.factory.MessageBodyFactory.initWriters(MessageBodyFactory.java:207)
at com.sun.jersey.core.spi.factory.MessageBodyFactory.init(MessageBodyFactory.java:163)
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:197)
at com.sun.jersey.api.client.Client.<init>(Client.java:187)
at com.sun.jersey.api.client.Client.<init>(Client.java:170)
at com.sun.jersey.api.client.Client.create(Client.java:679)
at com.mycompany.newwebapplicationproject.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:61)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:383)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
7 Jan, 2013 11:55:48 AM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$Text(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$Text(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader(com.sun.jersey.spi.inject.Injectable) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader(com.sun.jersey.spi.inject.Injectable) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$App(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$Text(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$Text(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$Text(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General(com.sun.jersey.spi.inject.Injectable,javax.ws.rs.ext.Providers) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter(com.sun.jersey.spi.inject.Injectable,com.sun.jersey.spi.inject.Injectable) at parameter index 0
SEVERE: Missing dependency for constructor public com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter(com.sun.jersey.spi.inject.Injectable,com.sun.jersey.spi.inject.Injectable) at parameter index 1
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
以下是我在 GWT 中的服务器端代码
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) throws IllegalArgumentException {
try {
getBaseURI();
ClientConfig config = new DefaultClientConfig();
Client client=Client.create(config);
WebResource service = client.resource(getBaseURI());
System.out.println(service.path("hello")
.accept(MediaType.TEXT_PLAIN).get(ClientResponse.class)
.toString());
System.out.println(service.path("hello")
.accept(MediaType.TEXT_PLAIN).get(String.class));
System.out.println(service.path("hello").accept(MediaType.TEXT_XML)
.get(String.class));
System.out.println(service.path("hello")
.accept(MediaType.TEXT_HTML).get(String.class));
System.out.println(service.path("generic").path("get")
.accept(MediaType.TEXT_HTML).get(String.class));
return "success";
} catch (Exception e) {
System.out.println(e.toString());
return e.toString();
}
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/RestWeb/webresources")
.build();
}
}
我使用以下包来继承所需的类
com.sun.jersey.jersey-core-1.4.0.jar
jersey-core-1.7.jar
jsr311-api-1.1.1.jar
以下是网络服务中的类
@Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
@GET
@Path("get")
@Produces(MediaType.TEXT_HTML)
public String getXml() {
return "xyz";
}
}
@Path("generic")
@WebService
public class GenericResource {
@Context
private UriInfo context;
@Context
private HttpServletResponse response;
private String content = "content";
/**
* Creates a new instance of GenericResource
*/
public GenericResource() {
}
/**
* Retrieves representation of an instance of
* com.mycompany.resource.GenericResource
*
* @return an instance of java.lang.String
*/
@GET
@Path("get")
@Produces(MediaType.TEXT_HTML)
public String getXml() {
System.out.println("\n\n\nGET\n\n\n");
//response.addHeader(content, content);
return this.content+" from get method";
}
/**
* PUT method for updating or creating an instance of GenericResource
*
* @param content representation for the resource
* @return an HTTP response with content of the updated or created resource.
*/
@PUT
@Path("put")
@Consumes("text/html,text/xml,text/plain")
public void putXml(@PathParam("content") String content) {
System.out.println("\n\n\nPUT\n\n\n");
response.addHeader(content, content);
this.content = this.content + content;
}
@POST
@Path("post")
@Consumes("text/html,text/xml,text/plain")
public void postXml(@PathParam("content") String content) {
System.out.println("\n\n\nPOST\n\n\n");
response.addHeader(content, content);
this.content = this.content + content;
}
}