我正在尝试使用 Jersey 实现 RESTful Web 服务客户端/JAX-RS:
public class MyClient implements Closeable {
private Client client;
private FizzResource fizzResource;
// Several other resources omitted for brevity.
// Ctor, getters and setters, etc.
@Override
public void close() throws Exception {
client.destroy();
client.getExecutorService().shutdown();
}
}
public class FizzResource {
private Client client;
public Fizz saveFizz(Fizz fizz) {
WebResource webResource = client.resource("whatever");
ClientResponse response = webResource.accept(???).post(???);
if(response.getStatus() != 200) {
// do something...
} else {
// do something else...
}
}
}
我的问题是我不想使用 JSON;相反,我想直接使用我的实体(例如 Fizz
)。我想使用Jackson自动执行 JSON 和我的实体之间的序列化(无需我在每个方法内显式执行转换),但我不知道这是如何可能/可行的。理想情况下,我的 saveFizz
方法可能如下所示:
public Fizz saveFizz(Fizz fizz) {
WebResource webResource = client.resource("whatever");
ClientResponse response = webResource.accept("application/json").post(fizz);
if(response.getStatus() != 200) {
throw new RuntimeException("Errors are bad, mkay?");
}
Fizz fizz = response.extractSomehow();
return fizz;
}
假设我的 Fizz
类已使用正确的 Jackson 注释(JsonProperty
等)进行注释。
有什么想法吗?
最佳答案
您使用的是 Jersey 1.x,所以请查看 the user guide for JSON/POJO support
第一件事:我们需要确保您拥有jersey-json
模块
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey-version}</version>
</dependency>
此模块将具有所需的 MessageBodyReader
和 MessageBodyWriter
,它们将在 JSON 中读取和写入 POJO
第二件事:我们需要确保启用 POJO 映射支持功能。既适用于服务器/应用程序,也适用于客户端
带有 web.xml 的服务器
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
服务器编程
public class MyApplication extends PackagesResourceConfig {
public MyApplication() {
getFeatures()..put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
}
}
客户端配置
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
Boolean.TRUE);
Client client = Client.create(clientConfig);
第三件事:我们只需要确保我们的资源方法被正确注释,并且我们正确地进行客户端调用(以允许发现正确的编写器/读取器)。
对于接受 JSON 的方法,应使用 @Consumed("application/json")
进行注释,如果该方法也生成 JSON 格式的响应,则还应使用 @Produces 进行注释(“应用程序/json”)
。因此,这取决于您的方法的语义,要包含哪些注释,可以是其中之一,也可以是两者。
对于客户端来说,只要我们正确配置,提取Java对象,只需调用Java类型的getXxx
即可。
public void testGetFizz() {
// Directly extact
Fizz fizz = r.path("fizz").accept("application/json").get(Fizz.class);
System.out.println(fizz);
// Extract from ClientResponse
ClientResponse response = r.path("fizz").
accept("application/json").get(ClientResponse.class);
Fizz fizz1 = response.getEntity(Fizz.class);
System.out.println(fizz1);
}
这是我用于测试的其他代码
@Path("/fizz")
public class FizzResource {
@POST
@Consumes("application/json")
public Response postFizz(Fizz fizz) {
System.out.println("==== Created Fizz ===");
System.out.println(fizz);
System.out.println("=====================");
return Response.created(null).build();
}
@GET
@Produces("application/json")
public Response getFizz() {
Fizz fizz = new Fizz(1, "fizz");
return Response.ok(fizz).build();
}
}
服务器配置
ResourceConfig resourceConfig = new PackagesResourceConfig("test.json.pojo");
resourceConfig.getFeatures().put(
JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
客户端配置
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
Boolean.TRUE);
Client client = Client.create(clientConfig);
r = client.resource(Main.BASE_URI);
// r = WebResource
关于java - 使用 Jackson 将 POJO 作为实体传递的 Jersey/JAX-RS 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27167816/