users@jersey.java.net

[Jersey] Re: how to rename XmlRootElement in JSON

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Mon, 30 May 2011 17:14:11 +0200

I guess, you have the Jackson Feature.WRAP_ROOT_VALUE set to true in
your object mapper.
You probably need that to properly serialize another type.

The bug [1] i was referring to earlier currently blocks
you from doing something like follows, so that you can use different
object mapper settings for various Java types:

@Provider
public class MyObjectMapperProvider implements
ContextResolver<ObjectMapper> {

     final ObjectMapper defaultObjectMapper;
     final ObjectMapper organicasObjectMapper;

     public MyObjectMapperProvider() {
         defaultObjectMapper = ... // Jackson Feature.WRAP_ROOT_VALUE
set to true here;
         organicasObjectMapper = ... // Jackson Feature.WRAP_ROOT_VALUE
set to false here;
     }

     @Override
     public ObjectMapper getContext(Class<?> type) {

         if (type == ListWrapperWithMap.class) {
             return organicasObjectMapper;
         } else {
             return defaultObjectMapper;
         }
     }
}

If the above was working fine, i guess your problem would be solved, right?

~Jakub

[1]http://java.net/jira/browse/JERSEY-726

On 05/30/2011 04:45 PM, Maxrunner wrote:
> This is what i get using the Map example, still the same problem, i get the
> name "ListWrapperWithMap" which i dont want, if i can hide this name and let
> only the "Organicas" name remain seems like a good idea for returning list
> of objects with the name i want....
>
> {
> "ListWrapperWithMap" : {
> "Organicas" : [ {
> "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
> } ]
> }
> }
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6419404.html
> Sent from the Jersey mailing list archive at Nabble.com.
>