Serialize javax.ws.rs Entity to json(将 javax.ws.rs 实体序列化为 json)
问题描述
我想用 org.glassfish.jersey
实现序列化为 Json
I want to serizalize to Json with org.glassfish.jersey
implementation
Map<String, String> entity = Maps.newHashMap();
entity.put("foo", "bar");
Response response = Response.status(Response.Status.OK)
.entity(entity)
.type(MediaType.APPLICATION_JSON).build();
System.out.println(response.getEntity());
此映射序列化为非标准 { foo: "bar" }
.我想在单元测试中测试这种行为.
This map serialize to non standard { foo: "bar" }
. I want to test this behaviour in unit test.
推荐答案
你不能这样测试.你在这里做什么
You can't test like this. What you are doing here
Response response = Response.status(Response.Status.OK)
.entity(entity)
.type(MediaType.APPLICATION_JSON).build();
正在构建出站响应.在 JAX-RS 框架中,我们发出响应后,例如
is building an outbound response. In the JAX-RS framework, after we send out a response, e.g.
@GET
@Produced(MediaType.APPLICATION_JSON)
public Response getResponse() {
...
return Response.status(Response.Status.OK)
.entity(entity)
.type(MediaType.APPLICATION_JSON).build();
}
它仍然需要通过 MessageBodyWriter
用于序列化为 JSON.
it still needs to through a MessageBodyWriter
for the serialization to JSON.
- 详细了解实体提供者
话虽如此,Jersey 有一个 测试框架,我们可以用于测试我们的资源方法.您可以在 Github 找到所有官方示例
That being said, Jersey has a Test Framework, we can use to test our resource methods. You can find all the official examples at the Github
一个样本(有一些改动):
这些是最低要求的 Maven 依赖项
These are the minimum required Maven dependencies
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
测试类
public class TestJSONResource extends JerseyTest {
@Override
protected TestContainerFactory getTestContainerFactory() {
return new GrizzlyTestContainerFactory();
}
@Path("test")
public static class TestResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getJson() {
Map<String, String> entity = Maps.newHashMap();
entity.put("foo", "bar");
Response response = Response.status(Response.Status.OK)
.entity(entity)
.type(MediaType.APPLICATION_JSON).build();
return response;
}
}
@Override
protected DeploymentContext configureDeployment() {
return DeploymentContext.builder(new ResourceConfig(TestResource.class))
.contextPath("context1/context2")
.build();
}
@Test
public void testGet() {
final WebTarget target = target("test");
final String s = target.request().get(String.class);
System.out.println(s);
}
}
jersey-media-json-jackson
提供了 MessageBodyWriter
和 MessageBodyReader
用于处理 JSON,这是为我们隐式注册的.
jersey-media-json-jackson
provides the MessageBodyWriter
and MessageBodyReader
for processing JSON, which is implicitly registered for us.
这篇关于将 javax.ws.rs 实体序列化为 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 javax.ws.rs 实体序列化为 json
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01