如何让CXF理解Map< String,< List< MyBean>>? [英] How do I get CXF to understand a Map<String, <List<MyBean>>?

查看:80
本文介绍了如何让CXF理解Map< String,< List< MyBean>>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的restful方法返回 Map< String,List< MyBean>> 但我无法弄清楚如何让CXF和JAXB将其序列化为XML。

My restful method returns a Map<String,List<MyBean>> but I can't figure out how to get CXF and JAXB to serialise this as XML.

我希望它看起来像这样(虽然我不是那么困扰它如何序列化,只要它在双方都有效);

I want it to look something like this (although I'm not all that bothered how it is serialised so long as it works on both sides);

<response>
  <items key="a">
    <item>
      ....
    </item>
    <item>
      ....
    </item>
  </items>
  <items key="b">
    <item>
      ....
    </item>
  </items>
</response>

如果我只返回地图我得到;

If I just return a Map I get;


[org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor]
找不到邮件正文编写者
用于响应类HashMap。

[org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor] No message body writer has been found for response class HashMap.

如果我尝试使用包装器对象,我得到;

If I try and use a wrapper object I get;


[org.apache.cxf.jaxrs.provider.AbstractJAXBProvider]
java.util.List是一个接口,
JAXB无法处理接口。

[org.apache.cxf.jaxrs.provider.AbstractJAXBProvider] java.util.List is an interface, and JAXB can't handle interfaces.

有什么建议吗?这只是一个CXF问题(我使用的是2.3.2版本)?我确信我在泽西岛也有过类似的事情。

Any suggestions? Is this just a CXF issue (I'm using version 2.3.2)? I'm sure I've had a similar thing working in Jersey.

推荐答案

没有什么比屏幕更远的时间了清楚一个人的想法。我在午餐时想到了这一点,答案是根本不使用 Map ,而是 List 列表包装。

There's nothing like some time away from the screen to clear one's mind. I figured this out over lunch and the answer is not to use a Map at all, but a List of List wrappers.

@XmlRootElement
public class MyResponse {

  private List<ItemListWrapper> items;

  //getters and setters
}

@XmlType
public class ItemListWrapper {

  private String key;
  private List<Item> items;

  @XmlAttribute
  public String getKey() {
    return this.key;
  }

  //rest of the getters and setters

}

@XmlType
public class Item {
  //just a bean
}

这篇关于如何让CXF理解Map&lt; String,&lt; List&lt; MyBean&gt;&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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