我正在嘗試使用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
從瀏覽器。任何幫助解決這個問題表示讚賞。 謝謝
問題是什麼?什麼不工作? – KatieK
問題是,客戶端無法訪問在瀏覽器中正常工作的url。我真的需要在今天工作。請幫忙。 – Preeti
您從客戶端發送了哪些HTTP標頭? – Yaniv