java 将 javax.ws.rs 实体序列化为 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27091834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:11:27  来源:igfitidea点击:

Serialize javax.ws.rs Entity to json

javajsonjerseyjax-rsjersey-2.0

提问by MariuszS

I want to serizalize to Json with org.glassfish.jerseyimplementation

我想通过org.glassfish.jersey实现序列化到 Json

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());

This map serialize to non standard { foo: "bar" }. I want to test this behaviour in unit test.

此映射序列化为非标准{ foo: "bar" }. 我想在单元测试中测试这种行为。

采纳答案by Paul Samsotha

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();

is building an outboundresponse. In the JAX-RS framework, after we send out a response, e.g.

正在构建出站响应。在 JAX-RS 框架中,在我们发出响应之后,例如

@GET
@Produced(MediaType.APPLICATION_JSON)
public Response getResponse() {
    ...
    return Response.status(Response.Status.OK)
                    .entity(entity)
                    .type(MediaType.APPLICATION_JSON).build();
}

it still needs to through a MessageBodyWriterfor the serialization to JSON.

它仍然需要通过 aMessageBodyWriter序列化为 JSON。

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

也就是说, Jersey 有一个Test Framework,我们可以用来测试我们的资源方法。您可以在 Github 上找到所有官方示例

A sample (with a few alterations):

一个示例(有一些改动):

These are the minimum required Maven dependencies

这些是最低要求的 Maven 依赖项

<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-Hymanson</artifactId>
        <version>2.13</version>
    </dependency>
</dependencies>

Test class

测试班

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-Hymansonprovides the MessageBodyWriterand MessageBodyReaderfor processing JSON, which is implicitly registered for us.

jersey-media-json-Hymanson提供用于处理 JSON的MessageBodyWriterMessageBodyReader,它为我们隐式注册。