users@jersey.java.net

[Jersey] Re: how to rename XmlRootElement in JSON

From: Maxrunner <joao.rossa_at_gmail.com>
Date: Wed, 25 May 2011 08:05:46 -0700 (PDT)

Hi! So im using Jackson Pojo with Jersey and my resource class method returns
a list of objects with the root name "List":

Resource Class:

@Path("/Organicas")
public class OrganicasResource {

        
        public OrganicasResource() {
        }

        @GET
        @Produces(MediaType.APPLICATION_JSON)
        @Path("/getAllOrganicas")
        //_at_JsonProperty("Organicas")
        public List<OrganicaMobileEntity> getOrganicas(){
                
                 List<OrganicaMobileEntity> organicas= new
ArrayList<OrganicaMobileEntity>();
                //dummy data
                organicas.add(fillOrganica(123,"Organica Power","2010-12-23"));
                organicas.add(fillOrganica(124,"Organica Mind","2000-05-21"));
                organicas.add(fillOrganica(125,"Organica Strength","2002-12-23"));
                organicas.add(fillOrganica(126,"Organica Stamina","2011-06-14"));
                organicas.add(fillOrganica(127,"Organica Luck","2006-02-01"));
                organicas.add(fillOrganica(128,"Organica Love","2003-04-07"));
                
                
                return organicas;
        }
        
        private OrganicaMobileEntity fillOrganica(int id, String designacao, String
dataAlteracao){
                
                OrganicaMobileEntity org= new OrganicaMobileEntity();
                org.setDataAlteracao(dataAlteracao);
                org.setDesignacao(designacao);
                org.setId(id);
                
                return org;
        }
}

Pojo Class:

@XmlRootElement( name = "Organica" )
public class OrganicaMobileEntity {

        private int id;
        private String designacao;
        private String dataAlteracao;
        
        
        public OrganicaMobileEntity() {
                super();
        }

        public OrganicaMobileEntity(int id, String designacao,
                        String dataAlteracao) {
                
                super();
                this.id = id;
                this.designacao = designacao;
                this.dataAlteracao = dataAlteracao;
        }

        public String getDataAlteracao() {
                return dataAlteracao;
        }
        
        public void setDataAlteracao(String dataAlteracao) {
                this.dataAlteracao = dataAlteracao;
        }
        
        public void setId(int id) {
                this.id = id;
        }
        
        public int getId() {
                return id;
        }
        
        public void setDesignacao(String designacao) {
                this.designacao = designacao;
        }
        public String getDesignacao() {
                return designacao;
        }

}

JSON result:

{
  "List" : [ { --> How to change this?
    "designacao" : "Organica Power",
    "dataAlteracao" : "2010-12-23",
    "id" : 123
  }, {
    "designacao" : "Organica Mind",
    "dataAlteracao" : "2000-05-21",
    "id" : 124
  }, {
    "designacao" : "Organica Strength",
    "dataAlteracao" : "2002-12-23",
    "id" : 125
  }, {
    "designacao" : "Organica Stamina",
    "dataAlteracao" : "2011-06-14",
    "id" : 126
  }, {
    "designacao" : "Organica Luck",
    "dataAlteracao" : "2006-02-01",
    "id" : 127
  }, {
    "designacao" : "Organica Love",
    "dataAlteracao" : "2003-04-07",
    "id" : 128
  } ]
}

How can i change the "List" root name value???

regards,


--
View this message in context: http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6403122.html
Sent from the Jersey mailing list archive at Nabble.com.