java 使用 Jersey 从客户端调用 RESTFul Web 服务的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13020408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issue calling RESTFul Web Service from Client using Jersey
提问by Preeti
I am trying to learn RESTFul web services using Jersey and following this example. I have created a sample service which is accessible at:
我正在尝试使用 Jersey 并按照此示例学习 RESTFul Web 服务。我创建了一个示例服务,可在以下位置访问:
http://localhost:8080/de.vogella.jersey.first/rest/hello.
I have created a client which calls this service but when I run this client I get an exception as follows:
我创建了一个调用此服务的客户端,但是当我运行此客户端时,出现如下异常:
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.access0(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)
Service class is
服务类是
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> ";
}
}
Client Code:
客户代码:
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();
}
}
The strange part is, I can get the right result if I hit
奇怪的是,如果我击中,我可以获得正确的结果
http://localhost:8080/de.vogella.jersey.first/rest/hello
from browser. Any help to resolve this issue is appreciated. Thanks
从浏览器。对解决此问题的任何帮助表示赞赏。谢谢
回答by Veerabhadra Rao Balla
As per your requirement in your service application you didn't mention the "Path" annotation before you creating the class, For instance:
根据您在服务应用程序中的要求,您在创建类之前没有提到“路径”注释,例如:
@Path("hello")
public class Hello {
}
That's only the problem in your service application.
那只是您的服务应用程序中的问题。
回答by Nitul
URL: de.vogella.jersey.first/rest/hello
网址:de.vogella.jersey.first/rest/hello
1) Make sure that you have given Servlet mappings to parse 'rest' from URL
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) Add @Path("hello")
declaration above method.
2) 添加@Path("hello")
上述方法的声明。
@Path("hello")
public String sayXMLHello() {}
回答by Justas
You should use:
你应该使用:
http://localhost:8080/hello
instead of
代替
http://localhost:8080/de.vogella.jersey.first/rest/hello
For testing purposes.
用于测试目的。
回答by Yogesh Prajapati
i am not getting any problem in this example if you want to run that example in another way than pass whole url which you pass in browser in following function you'll get result in your main class.
我在这个例子中没有遇到任何问题,如果你想以另一种方式运行这个例子,而不是在浏览器中传递你在以下函数中传递的整个 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;
}