杰克逊 + Jersey 返回根名称为“List”的列表

发布于 2024-11-09 03:56:36 字数 2576 浏览 4 评论 0原文

我目前正在尝试创建一个使用 Jackson 和 Jersey 序列化 POJO 对象的系统,这里是我的资源:

@Path("/OrganicasFuncao")

public class OrganicasFuncaoResource {

public OrganicasFuncaoResource() {
    // TODO Auto-generated constructor stub
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getAllOrganicasFuncao")

public List<OrganicaFuncaoMobileEntity> getOrganicasFuncao(){

     List<OrganicaFuncaoMobileEntity> organicas; organicas= new ArrayList<OrganicaFuncaoMobileEntity>();

    //dummy data
    organicas.add(fillOrganicaFuncao(111,123,"Função Assim","2010-12-23"));
    organicas.add(fillOrganicaFuncao(113,124,"Função Assada","2000-05-21"));
    organicas.add(fillOrganicaFuncao(113,125,"Função Meio termo","2002-12-23"));
    organicas.add(fillOrganicaFuncao(112,126,"Função Mal Passada","2011-06-14"));
    organicas.add(fillOrganicaFuncao(112,127,"Função Bem Passada","2006-02-01"));
    organicas.add(fillOrganicaFuncao(111,128,"Função Crua","2003-04-07"));
    organicas.add(fillOrganicaFuncao(113,127,"Função Queimadinha","2006-02-01"));
    organicas.add(fillOrganicaFuncao(113,128,"Função Tostada","2003-04-07"));


    return organicas;
}

private OrganicaFuncaoMobileEntity fillOrganicaFuncao(int id, int idOrg, String designacao, String dataAlteracao){

    OrganicaFuncaoMobileEntity orgFunc= new OrganicaFuncaoMobileEntity();
    orgFunc.setDataAlteracao(dataAlteracao);
    orgFunc.setDesignacao(designacao);
    orgFunc.setId(id);
    orgFunc.setIdOrganica(idOrg);

    return orgFunc;
}

}

这返回:

{ “列表” : [ { "designacao" : "Função Assim", “id有机”:123, "dataAlteracao" : "2010-12-23", “ID”:111 }, { "designacao" : "Função Assada", “id有机”:124, "dataAlteracao" : "2000-05-21", “ID”:113 }, { "designacao" : "Função Meio termo", “id有机”:125, "dataAlteracao" : "2002-12-23", “ID”:113 }, { "designacao" : "Função Mal Passada", “id有机”:126, "dataAlteracao" : "2011-06-14", “ID”:112 }, { "designacao" : "Função Bem Passada", “id有机”:127, "dataAlteracao" : "2006-02-01", “ID”:112 }, { "designacao" : "Função Crua", “id有机”:128, "dataAlteracao" : "2003-04-07", “ID”:111 }, { "designacao" : "Função Queimadinha", “id有机”:127, "dataAlteracao" : "2006-02-01", “ID”:113 }, { "designacao" : "Função Tostada", “id有机”:128, "dataAlteracao" : "2003-04-07", “ID”:113 }] 我如何

用我自己的一些名称更改“列表”名称?我尝试使用几个注释,如 @jsonTypeName 或 @jsonTypeInfo,但这不起作用......

Im currently trying to create a system for serializing POJO objects using Jackson and Jersey, here my resource:

@Path("/OrganicasFuncao")

public class OrganicasFuncaoResource {

public OrganicasFuncaoResource() {
    // TODO Auto-generated constructor stub
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getAllOrganicasFuncao")

public List<OrganicaFuncaoMobileEntity> getOrganicasFuncao(){

     List<OrganicaFuncaoMobileEntity> organicas; organicas= new ArrayList<OrganicaFuncaoMobileEntity>();

    //dummy data
    organicas.add(fillOrganicaFuncao(111,123,"Função Assim","2010-12-23"));
    organicas.add(fillOrganicaFuncao(113,124,"Função Assada","2000-05-21"));
    organicas.add(fillOrganicaFuncao(113,125,"Função Meio termo","2002-12-23"));
    organicas.add(fillOrganicaFuncao(112,126,"Função Mal Passada","2011-06-14"));
    organicas.add(fillOrganicaFuncao(112,127,"Função Bem Passada","2006-02-01"));
    organicas.add(fillOrganicaFuncao(111,128,"Função Crua","2003-04-07"));
    organicas.add(fillOrganicaFuncao(113,127,"Função Queimadinha","2006-02-01"));
    organicas.add(fillOrganicaFuncao(113,128,"Função Tostada","2003-04-07"));


    return organicas;
}

private OrganicaFuncaoMobileEntity fillOrganicaFuncao(int id, int idOrg, String designacao, String dataAlteracao){

    OrganicaFuncaoMobileEntity orgFunc= new OrganicaFuncaoMobileEntity();
    orgFunc.setDataAlteracao(dataAlteracao);
    orgFunc.setDesignacao(designacao);
    orgFunc.setId(id);
    orgFunc.setIdOrganica(idOrg);

    return orgFunc;
}

}

This returns:

{
"List" : [ {
"designacao" : "Função Assim",
"idOrganica" : 123,
"dataAlteracao" : "2010-12-23",
"id" : 111
}, {
"designacao" : "Função Assada",
"idOrganica" : 124,
"dataAlteracao" : "2000-05-21",
"id" : 113
}, {
"designacao" : "Função Meio termo",
"idOrganica" : 125,
"dataAlteracao" : "2002-12-23",
"id" : 113
}, {
"designacao" : "Função Mal Passada",
"idOrganica" : 126,
"dataAlteracao" : "2011-06-14",
"id" : 112
}, {
"designacao" : "Função Bem Passada",
"idOrganica" : 127,
"dataAlteracao" : "2006-02-01",
"id" : 112
}, {
"designacao" : "Função Crua",
"idOrganica" : 128,
"dataAlteracao" : "2003-04-07",
"id" : 111
}, {
"designacao" : "Função Queimadinha",
"idOrganica" : 127,
"dataAlteracao" : "2006-02-01",
"id" : 113
}, {
"designacao" : "Função Tostada",
"idOrganica" : 128,
"dataAlteracao" : "2003-04-07",
"id" : 113
} ]
}

How can i change the "List" name with some of my own? i tried using several annotations like @jsonTypeName or @jsonTypeInfo, but that didnt work....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2024-11-16 03:56:36

添加 Jackson 的依赖项。 Jackson 是 Jersey 支持的一个很好的 JSON 序列化器/反序列化器。它不需要对序列化/反序列化类进行任何注释,只需将它们定义为“结构”:

public class Person {
    public String name;
    public int age;
}

如果您让字段为 public,则不需要 getter 和 setter。您需要的依赖项可以在这里找到: http://mvnrepository.com/ search.html?query=com.fasterxml.jackson.core。无需额外配置,Jersey 会检测它是否在类路径上。

您遇到的问题可能是由 Jerseys 默认 OOTB JSON 序列化/反序列化引起的。

Add a dependency for Jackson. Jackson is a good JSON serializer/deserializer which Jersey supports. It does not require any annotations on the serialized/deserialized classes, just define them as "structs":

public class Person {
    public String name;
    public int age;
}

It does not need getters and setters if you let your fields be public. The dependencies you need can be found here: http://mvnrepository.com/search.html?query=com.fasterxml.jackson.core. No need for additional configuration, Jersey will detect it if it is on classpath.

The problem you are having is probably caused by Jerseys default OOTB JSON serialization/deserialization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文