users@jersey.java.net

[Jersey] Re: how to rename XmlRootElement in JSON

From: Maxrunner <joao.rossa_at_gmail.com>
Date: Mon, 30 May 2011 03:29:00 -0700 (PDT)

Ok here's my full content:

*The json was this:*

{
  "Organicas" : { ---> this is the wrapper class name
    "organicas" : [ {----> this is the type that is used to build the list
      "designacao" : null,
      "dataAlteracao" : "2010-12-23",
      "id" : 123,
      "active" : true
    }, {
      "designacao" : "Organica Mind",
      "dataAlteracao" : "2000-05-21",
      "id" : 124,
      "active" : false
    }, {
      "designacao" : "Organica Strength",
      "dataAlteracao" : "2002-12-23",
      "id" : 125,
      "active" : true
    }, {
      "designacao" : "Organica Stamina",
      "dataAlteracao" : "2011-06-14",
      "id" : 126,
      "active" : true
    }, {
      "designacao" : "Organica Luck",
      "dataAlteracao" : "2006-02-01",
      "id" : 127,
      "active" : false
    }, {
      "designacao" : "Organica Love",
      "dataAlteracao" : "2003-04-07",
      "id" : 128,
      "active" : false
    } ]
  }
}


*This the POJO data object:*

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

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

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

        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;
        }

        public boolean isActive() {
                return active;
        }
        
        public void setActive(boolean active) {
                this.active = active;
        }
}


*This the resource:*


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

        
        public OrganicasResource() {
        }

        @GET
        @Produces(MediaType.APPLICATION_JSON)
        @Path("/getAllOrganicas")
        public Organicas<OrganicaMobileEntity> getOrganicas(@Context
HttpServletRequest req){
                
                List<OrganicaMobileEntity> organicas= new ArrayList<OrganicaMobileEntity>();
                //dummy data
                organicas.add(fillOrganica(123,req.getParameter("lastUpdate"),"2010-12-23",true));
                organicas.add(fillOrganica(124,"Organica Mind","2000-05-21",false));
                organicas.add(fillOrganica(125,"Organica Strength","2002-12-23",true));
                organicas.add(fillOrganica(126,"Organica Stamina","2011-06-14",true));
                organicas.add(fillOrganica(127,"Organica Luck","2006-02-01",false));
                organicas.add(fillOrganica(128,"Organica Love","2003-04-07", false));
                System.out.println(req.getParameter("lastUpdate"));
                
                
                
                return new Organicas<OrganicaMobileEntity>(organicas);
        }
        
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        @Path("/getOrganica")
        public OrganicaMobileEntity retrieveOrganica(){
                
                return fillOrganica(123,"Organica Mind","2010-12-23",true);
        }
                
}


*Here's the Wrapper:*


public class Organicas<OrganicaMobileEntity>{
        
        private List<OrganicaMobileEntity> organicas;


        public Organicas() {
                super();
        }

        public Organicas(List<OrganicaMobileEntity> organicas) {
                this.organicas = organicas;
        }

        public List<OrganicaMobileEntity> getOrganicas() {
                return organicas; }

        public void setOrganicas(List<OrganicaMobileEntity> organicas) {
                this.organicas = organicas;
        }


}


*Here's my contextresolver:*


@Component
@Provider
public class JacksonObjectMapperProvider implements
ContextResolver<ObjectMapper>{

        
        public ObjectMapper getContext(Class<?> arg0) {

                AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
                AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
                AnnotationIntrospector pair = new
AnnotationIntrospector.Pair(primary, secondary);
                
                ObjectMapper result = new ObjectMapper();
                result.configure(Feature.INDENT_OUTPUT, true);//human readable indentation
                result.configure(Feature.WRAP_ROOT_VALUE, true);//show root element
                
                // make deserializer use JAXB annotations
                 result.getDeserializationConfig().setAnnotationIntrospector(pair);
                 // make serializer use JAXB annotations
                 result.getSerializationConfig().setAnnotationIntrospector(pair);
                
                return result;
        }
}



*And finally my web.xml servelet config:*

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>sample.hello.resources</param-value>
  </init-param>
  <init-param>

 <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
               <param-value>true</param-value>
      </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>



Regards,

On Fri, May 27, 2011 at 6:38 PM, Cowtowncoder [via Jersey] <
ml-node+6411899-481268136-73314_at_n2.nabble.com> wrote:

> On Fri, May 27, 2011 at 9:42 AM, Maxrunner <[hidden email]<http://user/SendEmail.jtp?type=node&node=6411899&i=0>>
> wrote:
> > So how can i use the xmlrootelement annotation on List type without
> > extending it?can you help me with this?ive lost too much time with this
> > already....
>
> Typically by using mix-in annotations,
> http://wiki.fasterxml.com/JacksonMixInAnnotations -- you associate
> annotations.
>
> Another possiblity is just a very simple wrapper bean. Something like:
>
> public class ListWrapper<T> {
> public List<T> rootNameIWant;
> }
>
> and return that instead of List. Or, if you want fully dynamic name
> just use Map<String, List<MyType>> by sub-classing:
>
> public class ListWrapperWithMap extends HashMap<String, List<ActualType>> {
>
> public ListWrapperWithMap(String rootName, List<ActualType> value) {
> this.put(rootName, value);
> }
> }
>
> and then:
>
> return new ListWrapperWithMap("rootName", listValue);
>
>
> -+ Tatu +-
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6411899.html
> To unsubscribe from how to rename XmlRootElement in JSON, click here<http://jersey.576304.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=6173292&code=am9hby5yb3NzYUBnbWFpbC5jb218NjE3MzI5MnwyMDYzODA1MDkw>.
>
>


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