通过JSR 311及其实现,我们有了一个通过REST公开Java对象的强大标准。然而,在客户端,似乎缺少了一些类似于Apache Axis for SOAP的东西——隐藏web服务并将数据透明地封送回Java对象的东西。
如何创建Java RESTful客户端?使用HTTPConnection和手动解析结果?或者专门的客户端,例如Jersey或Apache CXR?
通过JSR 311及其实现,我们有了一个通过REST公开Java对象的强大标准。然而,在客户端,似乎缺少了一些类似于Apache Axis for SOAP的东西——隐藏web服务并将数据透明地封送回Java对象的东西。
如何创建Java RESTful客户端?使用HTTPConnection和手动解析结果?或者专门的客户端,例如Jersey或Apache CXR?
当前回答
这是一个老问题(2008年),所以现在的选择比当时多得多:
Apache CXF有三个不同的REST客户端选项 泽西岛(上面提到过)。 Spring RestTemplate被Spring WebClient取代 公共HTTP客户端为旧的Java项目构建自己的。
更新(2020年仍然活跃的项目):
Apache HTTP Components (4.2) Fluent adapter - Basic replacement for JDK, used by several other candidates in this list. Better than old Commons HTTP Client 3 and easier to use for building your own REST client. You'll have to use something like Jackson for JSON parsing support and you can use HTTP components URIBuilder to construct resource URIs similar to Jersey/JAX-RS Rest client. HTTP components also supports NIO but I doubt you will get better performance than BIO given the short requestnature of REST. Apache HttpComponents 5 has HTTP/2 support. OkHttp - Basic replacement for JDK, similar to http components, used by several other candidates in this list. Supports newer HTTP protocols (SPDY and HTTP2). Works on Android. Unfortunately it does not offer a true reactor-loop based async option (see Ning and HTTP components above). However if you use the newer HTTP2 protocol this is less of a problem (assuming connection count is problem). Ning Async-http-client - provides NIO support. Previously known as Async-http-client by Sonatype. Feign wrapper for lower level http clients (okhttp, apache httpcomponents). Auto-creates clients based on interface stubs similar to some Jersey and CXF extensions. Strong spring integration. Retrofit - wrapper for lower level http clients (okhttp). Auto-creates clients based on interface stubs similar to some Jersey and CXF extensions. Volley wrapper for jdk http client, by google google-http wrapper for jdk http client, or apache httpcomponents, by google Unirest wrapper for jdk http client, by kong Resteasy JakartaEE wrapper for jdk http client, by jboss, part of jboss framework jcabi-http wrapper for apache httpcomponents, part of jcabi collection restlet wrapper for apache httpcomponents, part of restlet framework rest-assured wrapper with asserts for easy testing
关于选择HTTP/REST客户端的一个警告。确保检查你的框架栈为HTTP客户端使用了什么,它是如何执行线程的,如果它提供了一个客户端,最好使用相同的客户端。这是如果你使用像Vert。你可能想尝试使用它的后台客户端来参与框架提供的任何总线或反应器循环…否则,请为可能出现的有趣的线程问题做好准备。
其他回答
由于没有人提到,这里有另一个:Feign,由Spring Cloud使用。
我写了一个库,将java接口映射到远程JSON REST服务:
https://github.com/ggeorgovassilis/spring-rest-invoker
public interface BookService {
@RequestMapping("/volumes")
QueryResult findBooksByTitle(@RequestParam("q") String q);
@RequestMapping("/volumes/{id}")
Item findBookById(@PathVariable("id") String id);
}
泽西休息客户端的例子
添加依赖项:
<!-- Jersey -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
对于GetMethod,并传递两个参数:
Client client = Client.create();
WebResource webResource1 = client
.resource("http://localhost:10102/NewsTickerServices/AddGroup/"
+ userN + "/" + groupName);
ClientResponse response1 = webResource1.get(ClientResponse.class);
System.out.println("responser is" + response1);
GetMethod传递一个参数并获得一个List类型的响应:
Client client = Client.create();
WebResource webResource1 = client
.resource("http://localhost:10102/NewsTickerServices/GetAssignedUser/"+grpName);
//value changed
String response1 = webResource1.type(MediaType.APPLICATION_JSON).get(String.class);
List <String > Assignedlist = new ArrayList<String>();
JSONArray jsonArr2 = new JSONArray(response1);
for (int i =0;i<jsonArr2.length();i++){
Assignedlist.add(jsonArr2.getString(i));
}
上面它返回一个列表,我们将其作为list对象接受,然后将其转换为JSONArray,然后从JSONArray转换为list。
Post请求传递JSONObject作为参数:
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:10102/NewsTickerServices/CreateJUser");
// value added
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, mapper.writeValueAsString(user));
if (response.getStatus() == 500) {
context.addMessage(null, new FacesMessage("User already exist "));
}
正如我在这篇文章中提到的,我倾向于使用Jersey,它实现了JAX-RS,并附带了一个很好的REST客户机。好的事情是,如果您使用JAX-RS实现RESTful资源,那么Jersey客户端可以重用JAXB/XML/JSON/Atom等实体提供程序,因此您可以在服务器端重用与在客户端单元测试中使用相同的对象。
例如,下面是来自Apache Camel项目的一个单元测试用例,它从RESTful资源(使用JAXB对象Endpoints)查找XML有效负载。resource(uri)方法定义在这个基类中,它只使用Jersey客户机API。
e.g.
clientConfig = new DefaultClientConfig();
client = Client.create(clientConfig);
resource = client.resource("http://localhost:8080");
// let's get the XML as a String
String text = resource("foo").accept("application/xml").get(String.class);
顺便说一句,我希望未来版本的JAX-RS能像Jersey那样添加一个很好的客户端API。
如果您只希望调用REST服务并解析响应,则可以尝试REST Assured
// Make a GET request to "/lotto"
String json = get("/lotto").asString()
// Parse the JSON response
List<String> winnderIds = with(json).get("lotto.winners.winnerId");
// Make a POST request to "/shopping"
String xml = post("/shopping").andReturn().body().asString()
// Parse the XML
Node category = with(xml).get("shopping.category[0]");