Jaxb json missing brackets for one element array

让人想犯罪 __ 提交于 2019-12-07 05:46:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6386873/jaxb-json-missing-brackets-for-one-element-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!