繁体   English   中英

使用Jersey从客户端调用RESTFul Web Service的问题

[英]Issue calling RESTFul Web Service from Client using Jersey

我正在尝试使用Jersey学习RESTFul Web服务并遵循此示例 我创建了一个示例服务,可从以下位置访问:

    http://localhost:8080/de.vogella.jersey.first/rest/hello. 

我创建了一个调用此服务的客户端,但是当我运行此客户端时,我得到一个异常,如下所示:

   GET http://localhost:8080/de.vogella.jersey.first/rest/hello 
     returned a response status of 404 Not Found Exception in thread "main"  
   com.sun.jersey.api.client.UniformInterfaceException: 
     GET http://localhost:8080/de.vogella.jersey.first/rest/hello 
       returned a response status of 404 Not Found
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
at de.vogella.jersey.first.client.Test.main(Test.java:23)

服务类是

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

客户代码:

public class Test {
  public static void main(String[] args) {
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client.resource(getBaseURI());
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());  
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_XML).get(String.class));        
    private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/de.vogella.jersey.first").build();
  }
} 

奇怪的是,如果我击中,我可以得到正确的结果

    http://localhost:8080/de.vogella.jersey.first/rest/hello 

来自浏览器。 任何有助于解决此问题的帮助表示赞赏。 谢谢

根据您在服务应用程序中的要求,在创建类之前未提及“Path”注释,例如:

@Path("hello")
public class Hello {

}

这只是您的服务应用程序中的问题。

网址:de.vogella.jersey.first / rest / hello

1)确保您已经给出了Servlet映射来解析URL中的“rest”

  <servlet>
    <servlet-name>jersey-serlvet</servlet-name>
        ...
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

2)在方法上面添加@Path("hello")声明。

  @Path("hello")
  public String sayXMLHello() {}

我在这个例子中没有遇到任何问题,如果你想用另一种方式运行那个例子,而不是传递你在浏览器中传递的整个url,你将在你的主类中获得结果。

private String doGet(String url){
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource resource = client.resource(url);
        ClientResponse response = resource.type("application/x-www-form-urlencoded").get(ClientResponse.class);
        String en = response.getEntity(String.class);
        return en;
    }

你应该使用:

http://localhost:8080/hello

代替

http://localhost:8080/de.vogella.jersey.first/rest/hello

用于测试目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM