Jaxb json缺少一个元素数组的括号 [英] Jaxb json missing brackets for one element array

查看:177
本文介绍了Jaxb json缺少一个元素数组的括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

和我的ContextResolver:

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

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

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

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();

我得到{aw:{list:test}}

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

任何人都知道如何修复它?

Anyone know how to fix it?

推荐答案

我不太确定你是否正常工作所以我正在做出贡献。

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

我也绊倒了最近这个问题。我在stackoverflow中找到了一个帮助我的帖子,但更有帮助的是这个文章(介绍Jackson可能有所帮助)。

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).

我希望这对你也有帮助。对我来说,解决这个问题只需要5分钟。

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

这篇关于Jaxb json缺少一个元素数组的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆