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
				
        
 
            
        - 将log4j 1.2配置转换为log4j 2配置 2022-01-01
 - value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
 - 如何使用WebFilter实现授权头检查 2022-01-01
 - Jersey REST 客户端:发布多部分数据 2022-01-01
 - Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
 - Java包名称中单词分隔符的约定是什么? 2022-01-01
 - Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
 - C++ 和 Java 进程之间的共享内存 2022-01-01
 - 从 finally 块返回时 Java 的奇怪行为 2022-01-01
 - Eclipse 插件更新错误日志在哪里? 2022-01-01
 
						
						
						
						
						