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

2023-09-06 11:42:45 作者:我在乎

我想用 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 提供了 MessageBodyWriterMessageBodyReader 用于处理 JSON,这是为我们隐式注册的.

jersey-media-json-jackson provides the MessageBodyWriter and MessageBodyReader for processing JSON, which is implicitly registered for us.