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

时间:2022-06-17 21:43:00

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:

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

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

and my ContextResolver:

和我的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( “测试”);

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

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

@XmlRootElement  
public class Wrapper  
{  
    public ArrayWrapper aw;

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

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

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

Anyone know how to fix it?

谁知道怎么修它?

1 个解决方案

#1


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

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

#1


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

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