Jaxb json缺少一个元素数组的括号
我正在使用 JAXB/Jersey (1.3) 在 REST API 中将 java 转换为 json。 我读了很多关于这个问题的文章,我尝试了这个解决方案,它工作了一半
@XmlRootElement
public class ArrayWrapper
{
public List<String> list = new LinkedList<String>();
}
:我的 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("测试");
我得到 {"list":["test"]} 所以它可以工作,但是当我将 ArrayWrapper 包装在其他类中时它不起作用:
@XmlRootElement
public class Wrapper
{
public ArrayWrapper aw;
public Wrapper()
{
aw=new ArrayWrapper();
aw.list.add("test");
}
}
new Wrapper();
我得到 {"aw":{"list":"test"}}
有人知道如何修复它吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定你是否能正常工作,所以我贡献了自己的一份力量。
我最近也偶然发现了这个问题。我在 stackoverflow 中发现了一篇帖子对我有帮助,但更有用的是这个文章(介绍 Jackson 可能会有所帮助)。
我希望这对你也有帮助。对我来说,只用了 5 分钟就解决了这个问题。
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.