小编Gwe*_*wen的帖子

Jaxb json缺少一个元素数组的括号

我正在使用JAXB/Jersey(1.3)在REST API中将java转换为json.我读了很多关于这个问题,我尝试了这个解决方案,它工作了一半:

@XmlRootElement  
public class ArrayWrapper    
{  
        public List<String> list = new LinkedList<String>();  
}
Run Code Online (Sandbox Code Playgroud)

和我的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);
}  
Run Code Online (Sandbox Code Playgroud)

ArrayWrapper aw = new ArrayWrapper();
aw.list.add( "测试");

我得到{"list":["test"]}所以它可以工作但是当我在其他类中包装ArrayWrapper时它不起作用:

@XmlRootElement  
public class Wrapper  
{  
    public ArrayWrapper aw;

    public Wrapper()
    {
        aw=new ArrayWrapper();
        aw.list.add("test");
    }
}
Run Code Online (Sandbox Code Playgroud)

new Wrapper();
我得到{"aw":{"list":"test"}}

谁知道怎么修它?

json brackets jaxb

5
推荐指数
1
解决办法
4993
查看次数

标签 统计

brackets ×1

jaxb ×1

json ×1