Java Jersey 休息服务器 - 以 json 形式返回列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27937826/
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-08-11 05:20:14  来源:igfitidea点击:

Jersey rest server - Return list as json

javajsonrestjersey

提问by sjallamander

I'm experiencing a difference in the json structure of a returned list running the same code when running on Tomcat and Glassfish.

在 Tomcat 和 Glassfish 上运行时,运行相同代码的返回列表的 json 结构出现差异。

@XmlRootElement
public Class Person {
    private int personId;
    private String name;
}

@GET
@Path("/persons")
public Response getPersons()
{
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person(1, "John Smith"));
    persons.add(new Person(2, "Jane Smith"));

    GenericEntity<List<Person>> entity = new GenericEntity<List<Person>>(Lists.newArrayList<persons)) {};
    Return Response.ok(entity).build();
}

If returned in json format on tomcat (I'm running Tomcat on local machine) the result will be:

如果在 tomcat 上以 json 格式返回(我在本地机器上运行 Tomcat),结果将是:

[
    {
        "personId" : "1",
        "name" : "John Smith"
    },
    {
        "personId" : "2",
        "name" : "Jane Smith"
    }
]

And if returned in json format on Glassfish (running Glassfish on remote server) the result will be:

如果在 Glassfish 上以 json 格式返回(在远程服务器上运行 Glassfish),结果将是:

{
    "person" : 
    [
        {
            "personId" : "1",
            "name" : "John Smith"
        },
        {
            "personId" : "2",
            "name" : "Jane Smith"
        }
    ]
}

How can I control this format my self? I would prefer the array-format (as on Tomcat) if possible. Either way I want it to produce the same result.

我怎样才能自己控制这种格式?如果可能,我更喜欢数组格式(如在 Tomcat 上)。无论哪种方式,我都希望它产生相同的结果。

Edit:

编辑:

Dependencies: jersey-container-servlet (2.14), jersey-server (2.14), jersey-media-moxy (2.14), javax.servlet-api (3.0.1)

依赖:jersey-container-servlet (2.14), jersey-server (2.14), jersey-media-moxy (2.14), javax.servlet-api (3.0.1)

Version of Glassfish: 3.1.2.2 (build 5)

Glassfish 版本:3.1.2.2(构建 5)

Edit 2: It's a problem with Jersey 2.14 and Glassfish 3.x

编辑 2:这是 Jersey 2.14 和 Glassfish 3.x 的问题

I just installed Glassfish 3 and 4 and deployed the rest app to check the response. It resulted in different json structure when returning a list. The response from Glassfish 4 was identical to the result I got when running on Tomcat.

我刚刚安装了 Glassfish 3 和 4 并部署了其余的应用程序来检查响应。返回列表时会导致不同的 json 结构。Glassfish 4 的响应与我在 Tomcat 上运行时得到的结果相同。

回答by FrAn

Try add the response mediatype annotation and try with the List not GenericEntity like this:

尝试添加响应 mediatype 注释并尝试使用 List 而不是 GenericEntity 像这样:

 @GET
 @Path("/persons")
 @Produces({ MediaType.APPLICATION_JSON })
 public Response getPersons()
 {
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person(1, "John Smith"));
    persons.add(new Person(2, "Jane Smith"));

 Return Response.ok(persons).build();
 }