开发者

Jackson + Jersey returns List with root name "List"

开发者 https://www.devze.com 2023-03-08 13:47 出处:网络
Im currently trying to create a system for serializing POJO objects using Jackson and Jersey, here my resource:

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 }, { 开发者_如何学JAVA "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....


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消