如何为外部世界 REST 服务创建 REST 客户端
How to create REST client for external world REST services
如何使用 JAX-RS Client fluent API 为外部世界 REST 服务创建休息客户端?
例如假设服务 return 是一个具有两个字段名称和年龄的人对象。
无论我遇到什么 examples/tutorials,他们都按照下面的代码片段做同样的事情,或者他们在同一个项目中开发客户端,以便用 person.class 替换 String.class。我应该如何创建将 return 我 pojo 的独立客户端。
String entity = client.target("http://example.com/rest")
.path("resource/helloworld")
.queryParam("greeting", "Hi World!")
.request(MediaType.TEXT_PLAIN_TYPE)
.header("some-header", "true")
.get(String.class);
请求实体并将其映射到 Java class
例如,考虑在 http://example.com/api/people/1
处执行 GET
请求时,您使用的 REST API 提供以下 JSON:
{
"name": "John Doe",
"age": 25
}
上面的JSON可以映射成Javaclass,定义如下:
public class Person {
private String name;
private Integer age;
// Constructor, getters and setters omitted
}
使用 JAX-RS 客户端 API,可以按如下方式请求 JSON,将请求的实体映射到 Person
class:
Client client = ClientBuilder.newClient();
Person person = client.target("http://example.com/api")
.path("people").path("1")
.request(MediaType.APPLICATION_JSON)
.get(Person.class);
String name = person.getName();
Integer age = person.getAge();
JAX-RS 客户端 API 是 JAX-RS 2.0 specification and the reference implementation is Jersey.
的一部分
为了解析JSON,Jersey自带了一套扩展模块,用于多个框架,为JSON处理and/or JSON-to-[=49提供支持=] 绑定。球衣支持 MOXy, JSON-P, Jackson and Jettison. For more details, have a look at the documentation.
手动解析 JSON
如果出于某种原因,您需要手动解析请求的实体,您可以将请求的实体存储在 String
:
Client client = ClientBuilder.newClient();
String json = client.target("http://example.com/api")
.path("people").path("1")
.request(MediaType.APPLICATION_JSON)
.get(String.class);
然后可以手动解析请求的实体,例如,Jackson:
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
String name = node.path("name").asText();
Integer age = node.path("age").asInt();
要手动解析 JSON,您还可以考虑 Gson。
备选方案
有关如何使用 Java 使用 REST API 的替代方法,请查看 。
如何使用 JAX-RS Client fluent API 为外部世界 REST 服务创建休息客户端?
例如假设服务 return 是一个具有两个字段名称和年龄的人对象。
无论我遇到什么 examples/tutorials,他们都按照下面的代码片段做同样的事情,或者他们在同一个项目中开发客户端,以便用 person.class 替换 String.class。我应该如何创建将 return 我 pojo 的独立客户端。
String entity = client.target("http://example.com/rest")
.path("resource/helloworld")
.queryParam("greeting", "Hi World!")
.request(MediaType.TEXT_PLAIN_TYPE)
.header("some-header", "true")
.get(String.class);
请求实体并将其映射到 Java class
例如,考虑在 http://example.com/api/people/1
处执行 GET
请求时,您使用的 REST API 提供以下 JSON:
{
"name": "John Doe",
"age": 25
}
上面的JSON可以映射成Javaclass,定义如下:
public class Person {
private String name;
private Integer age;
// Constructor, getters and setters omitted
}
使用 JAX-RS 客户端 API,可以按如下方式请求 JSON,将请求的实体映射到 Person
class:
Client client = ClientBuilder.newClient();
Person person = client.target("http://example.com/api")
.path("people").path("1")
.request(MediaType.APPLICATION_JSON)
.get(Person.class);
String name = person.getName();
Integer age = person.getAge();
JAX-RS 客户端 API 是 JAX-RS 2.0 specification and the reference implementation is Jersey.
的一部分为了解析JSON,Jersey自带了一套扩展模块,用于多个框架,为JSON处理and/or JSON-to-[=49提供支持=] 绑定。球衣支持 MOXy, JSON-P, Jackson and Jettison. For more details, have a look at the documentation.
手动解析 JSON
如果出于某种原因,您需要手动解析请求的实体,您可以将请求的实体存储在 String
:
Client client = ClientBuilder.newClient();
String json = client.target("http://example.com/api")
.path("people").path("1")
.request(MediaType.APPLICATION_JSON)
.get(String.class);
然后可以手动解析请求的实体,例如,Jackson:
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
String name = node.path("name").asText();
Integer age = node.path("age").asInt();
要手动解析 JSON,您还可以考虑 Gson。
备选方案
有关如何使用 Java 使用 REST API 的替代方法,请查看