在调用休息Web服务时使用jersey客户端库的问题

时间:2011-01-14 14:45:19

标签: web-services rest jersey

我有一个使用jersey客户端框架调用rest web服务的servlet,这是客户端代码 -

response.setContentType("application/json");
    String adCategoryId = request.getParameter("adCategoryId");
    String requirement = request.getParameter("requirement");
    Client client = Client.create();
    WebResource wr = client.resource("http://localhost:8080/com.pandora.services/service");
    String adResult = wr.path("search-ad").path(requirement).path(adCategoryId).get(String.class);

服务端的代码就是这个 -

  

@Path( “/服务”)   公共课堂服务{   @得到   @Path( “/搜索广告/ {需要} / {查询}”)
  public String searchAd(@PathParam(“need”)字符串要求,@ PathParam(“query”)字符串id)       {           System.out.println(“内部服务”);           字符串adResult =“”;

    AdServiceProvider ad = new AdServiceProviderImpl();
    List<AdBean> adBean = ad.getAdById(Long.parseLong(id.trim()),requirement);
    adResult = gson.toJson(adBean);
    System.out.println(adResult);
    return adResult;

}

}

然而,当我拨打电话时...我收到以下错误 - 更新

  

servlet的Servlet.service()Resteasy引发了异常   显示java.lang.NullPointerException       在org.jboss.resteasy.plugins.server.servlet.HttpServletInputMessage。(HttpServletInputMessage.java:60)       在org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.createHttpRequest(HttpServletDispatcher.java:71)       在org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.createResteasyHttpRequest(HttpServletDispatcher.java:60)       在org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:197)       在org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)       在org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:820)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)       在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)       在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:588)       在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489)       在java.lang.Thread.run(未知来源)   2011年1月14日下午9:58:05 org.apache.catalina.core.StandardWrapperValve调用   严重:servlet测试的Servlet.service()引发了异常   com.sun.jersey.api.client.UniformInterfaceException:GET http://localhost:8080/com.pandora.services/service/hello返回的响应状态为500       在com.sun.jersey.api.client.WebResource.handle(WebResource.java:565)       在com.sun.jersey.api.client.WebResource.get(WebResource.java:182)       在com.pandora.client.servlets.test.doPost(test.java:45)       在com.pandora.client.servlets.test.doGet(test.java:33)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:707)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:820)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)       在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)       在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:588)       在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489)       在java.lang.Thread.run(未知来源)

...谢谢

1 个答案:

答案 0 :(得分:0)

就在两天前,关于Apache Wink的是similar question

问题的根本原因:请求中的媒体类型不正确。它应该是*/*,而不仅仅是*。例外情况非常明确。
在最新版本的Wink中,对于不正确的媒体类型格式变得更加可以原谅。我不知道泽西岛怎么样。但IMO最好解决根本问题 我猜你的情况下不正确的标题是“接受”。您可以使用任何HTTP Sniffer(例如Fiddler)对其进行验证。因此,如果您在请求中设置了正确的Accept标头,则应该解决问题。