5

I'm using JAXB/Jersey (1.3) to convert java to json in a REST API. I read a lot about this problem, I tryed this solution, it work a half:

@XmlRootElement  
public class ArrayWrapper    
{  
        public List<String> list = new LinkedList<String>();  
}

and my ContextResolver:

@Provider  
public class JAXBContextResolver implements ContextResolver<JAXBContext> {  

        private JAXBContext context;

        private Class[] types = {ArrayWrapper.class,Wrapper.class};

        public JAXBContextResolver() throws Exception {

            MappedBuilder builder = JSONConfiguration.mapped();
            builder.arrays("list");
            builder.rootUnwrapping(true);
            this.context = new JSONJAXBContext(builder.build(), types);
}  

ArrayWrapper aw=new ArrayWrapper();
aw.list.add("test");

I get {"list":["test"]} so it works but when I wrapp ArrayWrapper in an other class it don't work:

@XmlRootElement  
public class Wrapper  
{  
    public ArrayWrapper aw;

    public Wrapper()
    {
        aw=new ArrayWrapper();
        aw.list.add("test");
    }
}

new Wrapper();
I get {"aw":{"list":"test"}}

Anyone know how to fix it?

bdoughan
  • 147,609
  • 23
  • 300
  • 400
Gwen
  • 51
  • 1
  • 2
  • I used this [solution](http://stackoverflow.com/questions/2199453/how-can-i-customize-serialization-of-a-list-of-jaxb-objects-to-json), the first answer to the post works – Gwen Jun 20 '11 at 08:55

1 Answers1

0

I am not quite sure if you got it working so I am contributing my bit.

I also stumbled upon this issue recently. I found a post in stackoverflow that helped me, but even more helpful was this article (introducing Jackson might help).

I hope this helps you, too. For me it was a matter of 5 minutes to fix the issue.

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135