[英]How to consume RESTful web-service?
在本教程中,编写了如何创建REST服务以及如何使用它。 我被举个例子弄糊涂了。 那里我们需要在客户端jersey.jar
编写如下代码:
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
为什么客户需要知道Web服务的实现方式(球衣或以后的实现方式)? 为什么客户端不使用简单的InputStream
来使用它?
在此特定教程中,您将使用Jersey CLIENT与RESTful服务进行交互。
您也可以通过手动创建HTTP请求并接收响应并进行相应的解析来直接与服务进行交互(http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html)。
最终,Jersey客户端只是对此的一种抽象,以使其更易于使用。
String URL ="http://localhost:8080/MyWServices/REST/WebService/";
String ws_method_name = "getManagerListByRoleID";
String WS_METHOD_PARAMS = "";
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(URL + ws_method_name + WS_METHOD_PARAMS);
String text = null;
try {
HttpResponse httpResponse = httpClient
.execute(httpGet, httpContext);
HttpEntity entity = httpResponse.getEntity();
text = getASCIIContentFromEntity(entity);
}catch(Exception e){
e.printStackTrace();
}
使用Restful Web服务的最简单方法是使用Spring RestTemplate。 http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/client/RestTemplate.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.