杰克逊 + Jersey 返回根名称为“List”的列表
我目前正在尝试创建一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加 Jackson 的依赖项。 Jackson 是 Jersey 支持的一个很好的 JSON 序列化器/反序列化器。它不需要对序列化/反序列化类进行任何注释,只需将它们定义为“结构”:
如果您让字段为 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":
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.