3

我正在尝试使用 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 

从浏览器。任何解决此问题的帮助表示赞赏。谢谢

4

4 回答 4

1

网址: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() {}
于 2014-06-16T09:50:55.783 回答
1

根据您在服务应用程序中的要求,您在创建类之前没有提到“路径”注释,例如:

@Path("hello")
public class Hello {

}

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

于 2013-03-26T13:59:51.070 回答
0

如果您想以另一种方式运行该示例,而不是通过在浏览器中通过以下函数传递的整个 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;
    }
于 2012-11-27T06:32:25.267 回答
0

你应该使用:

http://localhost:8080/hello

代替

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

用于测试目的。

于 2016-06-21T14:13:30.317 回答