通过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?
当前回答
试着看看http-rest-client
https://github.com/g00dnatur3/http-rest-client
这里有一个简单的例子:
RestClient client = RestClient.builder().build();
String geocoderUrl = "http://maps.googleapis.com/maps/api/geocode/json"
Map<String, String> params = Maps.newHashMap();
params.put("address", "beverly hills 90210");
params.put("sensor", "false");
JsonNode node = client.get(geocoderUrl, params, JsonNode.class);
库为您处理json的序列化和绑定。
这是另一个例子,
RestClient client = RestClient.builder().build();
String url = ...
Person person = ...
Header header = client.create(url, person);
if (header != null) System.out.println("Location header is:" + header.value());
最后一个例子,
RestClient client = RestClient.builder().build();
String url = ...
Person person = client.get(url, null, Person.class); //no queryParams
干杯!
其他回答
我使用Apache HTTPClient来处理所有HTTP方面的事情。
我为XML内容编写XML SAX解析器,用于将XML解析为对象模型。我认为Axis2还公开了XML -> Model方法(Axis 1隐藏了这一部分,令人恼火)。XML生成器非常简单。
在我看来,它不需要很长时间来编码,而且非常高效。
试着看看http-rest-client
https://github.com/g00dnatur3/http-rest-client
这里有一个简单的例子:
RestClient client = RestClient.builder().build();
String geocoderUrl = "http://maps.googleapis.com/maps/api/geocode/json"
Map<String, String> params = Maps.newHashMap();
params.put("address", "beverly hills 90210");
params.put("sensor", "false");
JsonNode node = client.get(geocoderUrl, params, JsonNode.class);
库为您处理json的序列化和绑定。
这是另一个例子,
RestClient client = RestClient.builder().build();
String url = ...
Person person = ...
Header header = client.create(url, person);
if (header != null) System.out.println("Location header is:" + header.value());
最后一个例子,
RestClient client = RestClient.builder().build();
String url = ...
Person person = client.get(url, null, Person.class); //no queryParams
干杯!
正如我在这篇文章中提到的,我倾向于使用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。
您可以使用标准的Java SE api:
private void updateCustomer(Customer customer) {
try {
URL url = new URL("http://www.example.com/customers");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/xml");
OutputStream os = connection.getOutputStream();
jaxbContext.createMarshaller().marshal(customer, os);
os.flush();
connection.getResponseCode();
connection.disconnect();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
或者您可以使用JAX-RS实现(如Jersey)提供的REST客户机api。这些api更容易使用,但在类路径上需要额外的jar。
WebResource resource = client.resource("http://www.example.com/customers");
ClientResponse response = resource.type("application/xml");).put(ClientResponse.class, "<customer>...</customer.");
System.out.println(response);
有关更多信息,请参阅:
http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-55.html
由于没有人提到,这里有另一个:Feign,由Spring Cloud使用。