2012-10-22 27 views
3

我正在尝试使用Jersey和以下this example学习RESTFul Web服务。我创建了一个示例服务,其可在:从客户端使用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 

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

+0

问题是什么?什么不工作? – KatieK

+0

问题是,客户端无法访问在浏览器中正常工作的url。我真的需要在今天工作。请帮忙。 – Preeti

+0

您从客户端发送了哪些HTTP标头? – Yaniv

回答

0

如果你想以另一种方式运行这个例子,而不是传递整个网址,你在浏览器中通过下面的函数,你会得到你的主类的结果,我没有得到任何问题。

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

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

@Path("hello") 
public class Hello { 

} 

这只是在你的服务应用程序的问题。

1

网址:de.vogella.jersey.first/REST /你好

1)确保你已经给Servlet的映射,以便从URL

<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() {} 
+0

'jersey-serlvet'可能不会工作;) – Lambart

0

,您应该使用:的

http://localhost:8080/hello 

代替

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

出于测试目的。