0

我创建了一个 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;
    }
}
4

3 回答 3

0

通用路径可能有问题。

System.out.println(service.path("generic").path("get")
                    .accept(MediaType.TEXT_HTML).get(String.class));

你能显示通用路径的网络服务的映射吗?

于 2013-01-07T07:34:18.820 回答
0

您的 PUT 和 POST 方法应采用表示请求正文的参数。相反,他们期望一个路径参数content

@PUT
@Consumes("text/html,text/xml,text/plain")
public void putXml(@PathParam("content") String content) 
于 2013-01-07T17:13:57.937 回答
0

我得到了解决方案 GWT 不支持某些软件包。我发现 GWT 中的 RequestBuilder 类可以连接 REST Web 服务,我使用 RequestBuilder 类来连接 REST Web 服务。但是我没有从 webservice 得到任何东西,因为我的 GWT 和 REST 应用程序有不同的端口和域。它通过在标题中设置 Access-Control-Allow-Orgin 来克服

以下是我的客户端代码(GWT)

    public void onModuleLoad() {
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
         "http://localhost:8080/RestWeb/webresources/generic/post/"
         +"haiiiiiii");
        builder.setCallback(new RequestCallback() {

         @Override
         public void onResponseReceived(Request request,
        Response response) {

         Window.alert("onResponseReceived"
        + response.getStatusCode() + response.getText());

         }

         @Override
         public void onError(Request request, Throwable exception) {

         }
         });
        builder.setHeader("Content-Type", "text/plain");
         builder.setHeader("Access-Control-Allow-Methods",
         "PUT, GET, POST, DELETE, OPTIONS");
         builder.setHeader("Access-Control-Allow-Headers",
         "Content-Type");
         builder.setHeader("Access-Control-Allow-Origin",
         "http://127.0.0.1:8888");
         try {
         builder.send();
         } catch (RequestException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }
}

以下是我的服务器端代码

 @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;
    }
}

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;

/**
 *
 * @author SPC
 */
public class CorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
      //  if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
            // CORS "pre-flight" request
                response.addHeader("Content-Type",
                "multipart/form-data,text/plain,text/html");
                response.addHeader("Connection", "keep-alive");
            response.addHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8888");
            response.addHeader("Access-Control-Allow-Methods",  "PUT, GET, POST, DELETE, OPTIONS");
            response.addHeader("Access-Control-Allow-Headers","origin, access-control-allow-methods, content-type, access-control-allow-origin, access-control-allow-headers");
            response.addHeader("Access-Control-Max-Age", "1800");//30 min
       // }
        filterChain.doFilter(request, response);
    }
}
于 2013-01-15T13:24:52.220 回答