简体   繁体   English

使用 JsonObject 作为 Jersey 2 响应的实体

[英]using JsonObject as entity for Jersey 2 response

I have this simple code:我有这个简单的代码:

package com.example

import javax.json.Json;
import javax.json.JsonObject;

...

@Path("/")
public class Resource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response defaultEntry() {
        JsonObject result = (Json.createObjectBuilder()
                             .add("hello", "world")
                             .build());

        return Response.status(200).entity(result.toString()).build();
    }
}

I am new to Java, could someone please explain why, if I omit the call to result.toString() and simply pass result to .entity (like so: return Response.status(200).entity(result).build() ), I get JSON on the client that includes type information etc, but not what I expected:我是 Java 的新手,如果我省略对result.toString()的调用并简单地将result传递给.entity (像这样: return Response.status(200).entity(result).build() ), 我在客户端上得到 JSON,其中包括类型信息等,但不是我所期望的:

{"hello":{"chars":"world","string":"world","valueType":"STRING"}}

what is the intention of this?这是什么意图? How is passing JsonObject to it different from passing a string?JsonObject传递给它与传递字符串有何不同?

Also, I did not find Response.entity method in the documentation ( https://jersey.java.net/apidocs/2.11/jersey/javax/ws/rs/core/Response.html ).另外,我没有在文档中找到Response.entity方法( https://jersey.java.net/apidocs/2.11/jersey/javax/ws/rs/core/Response.html )。 I copied this code from a tutorial, that did not explain properly what is going on...我从教程中复制了这段代码,但没有正确解释发生了什么......

I wish I had a better answer for you, this more of a hint until a better answer arrives. 希望我对您有一个更好的答案,这更多的是提示,直到更好的答案到来为止。 There are a few moving parts here. 这里有一些运动部件。 JsonObject is an interface. JsonObject是一个接口。 Its implementation is not described. 没有描述其实现。 Furthermore there is a Json serializer that is turning your returned objects into Json text. 此外,还有一个Json序列化程序将您返回的对象转换为Json文本。 It is both these things together that is leading to this Json schema output. 这两件事共同导致了Json模式的输出。 When you did the .toString() variation, the serializer just returned the String as is. 当您执行.toString()变体时,序列化程序仅按原样返回String。 But when you return the JsonObject now you have these two dynamics at play, the implementation of the JsonObject and the implementation of the serializer. 但是,当您现在返回JsonObject时,将同时具有这两个动态因素,即JsonObject的实现和序列化器的实现。 Since you are using Jersey 2.0 you could be using Jackson, Moxy, or Jettison serializers. 由于使用的是Jersey 2.0,因此可以使用Jackson,Moxy或Jettison序列化程序。 These all might have different output when serializing the JsonObject, but we would have to test to be sure. 序列化JsonObject时,所有这些都可能具有不同的输出,但是我们必须进行测试才能确定。 Furthermore, the JsonObject implementation might be configured in a way that when serialized by your chosen serializer leads to its output being a Json schema, versus just regular Json. 此外,可以通过以下方式配置JsonObject实现:在您选择的序列化程序进行序列化时,导致其输出为Json模式,而不是常规Json。 This can be done using annotations that are specific to the chosen Json serializer. 这可以使用特定于所选Json序列化器的注释来完成。

In my career I have used multiple Json serializers. 在我的职业生涯中,我使用了多个Json序列化器。 Jackson is probably the most popular one out there. 杰克逊可能是那里最受欢迎的人。 But I have also used Gson extensively. 但是我也广泛使用了Gson。 In one project we configured Gson in some way where its serialized Json output came out as a Json schema when serializing POJO's. 在一个项目中,我们以某种方式配置了Gson,在序列化POJO时,它的序列化Json输出作为Json模式出现。 So its not far fetched to have a Json serializer output Json schema under certain conditions. 因此在特定条件下拥有Json序列化器输出Json模式并不难。

When serializing POJO's (aka Java Beans), you expect a regular Json output when using default settings of your serializer on a Java Bean. 序列化POJO(也称为Java Bean)时,在Java Bean上使用序列化程序的默认设置时,您期望有常规的Json输出。 But when sending back objects that could have complex interworkings with specific Json serializers you may get varying Json output. 但是,当发送回可能与特定Json序列化器进行复杂交互操作的对象时,您可能会得到变化的Json输出。

In this situation you would have to run tests to dive deeper into what is going on. 在这种情况下,您将必须运行测试以更深入地了解正在发生的事情。 For example, I would first test out the serializer against a POJO that matches the JsonObject you created. 例如,我将首先针对与您创建的JsonObject匹配的POJO测试序列化程序。 Then I would also test out other Json serializers on the same JsonObject. 然后,我还将在同一JsonObject上测试其他Json序列化器。 See if you can pick up on a pattern. 看看您是否可以选择一种图案。

The datatype module jackson-datatype-jsr353 provides support for javax.json types.数据类型模块jackson-datatype-jsr353提供对javax.json类型的支持。 The page includes instructions how to add the dependency and register the module with Jackson's ObjectMapper .该页面包含如何添加依赖项以及如何使用 Jackson 的ObjectMapper注册模块的说明。

Starting with Jackson 2.11.0, the module moved under the umbrella of jackson-datatypes-misc .从 Jackson 2.11.0 开始,该模块移至jackson-datatypes-misc的保护伞下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM