将javax.ws.rs实体序列化为json [英] Serialize javax.ws.rs Entity to json

查看:371
本文介绍了将javax.ws.rs实体序列化为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 org.glassfish.jersey 实现

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.

  • Read more about Entity Providers

话虽如此,泽西岛有一个测试框架,我们可以用来测试我们的资源方法。您可以在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆