java 如何让 CXF 理解 Map<String, <List<MyBean>>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6137665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:31:31  来源:igfitidea点击:

How do I get CXF to understand a Map<String, <List<MyBean>>?

javajaxbcxfjax-rs

提问by Qwerky

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.

我的 restful 方法返回 aMap<String,List<MyBean>>但我不知道如何让 CXF 和 JAXB 将其序列化为 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 MapI get;

如果我只是返回一个Map我得到;

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

[org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor] 没有找到响应类 HashMap 的消息正文编写器。

If I try and use a wrapper object I get;

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

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

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

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.

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

采纳答案by Qwerky

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 Mapat all, but a Listof Listwrappers.

没有什么比离开屏幕一段时间更能让人清醒的了。我想通了这一点在午餐和答案是不使用Map的,而是一个ListList包装。

@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
}

回答by happymeal

the error occurred because CXF does not know what are the keys and what are the objects. you will need to use aegis to specify the java Map to XML bindings.

发生错误是因为 CXF 不知道什么是键,什么是对象。您将需要使用 aegis 来指定 Java Map to XML 绑定。

refer to the section "Handling Maps" in this link: xfire mapping and collections. (i couldn't find the aegis Map/Collection documentation for CXF so here's the documentation from xfire instead. the aegis configuration should be similar since xfire 2.0 is CXF)

请参阅此链接中的“处理地图”部分:xfire 映射和集合。(我找不到 CXF 的 aegis Map/Collection 文档,所以这里是来自 xfire 的文档。因为 xfire 2.0 是 CXF,所以 aegis 配置应该类似)

alternatively, just stick to Collections and Lists.

或者,只需坚持使用集合和列表。

回答by Ahson Imtiaz

You can provide the configuration for msg-handlers/providers as follows if you are using CXF 2.3.2 with Spring:

如果您将 CXF 2.3.2 与 Spring 一起使用,您可以为 msg-handlers/providers 提供如下配置:

<jaxrs:server id="restContainer" address="/">
    <jaxrs:serviceBeans>
        <ref bean="yourRestfulServiceBeanId"/>
    </jaxrs:serviceBeans>
    <jaxrs:extensionMappings>
        <entry key="xml" value="application/xml"/>
    </jaxrs:extensionMappings>
    <jaxrs:providers>
        <ref bean="jaxbXmlProvider"/>
    </jaxrs:providers>
</jaxrs:server>

<!-- Webservice message handlers -->  
<bean id="jaxbXmlProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
    <property name="jaxbElementClassMap" ref="propertiesMap"/>
</bean>

<util:map id="propertiesMap">
    <entry key="jaxb.formatted.output">
        <value type="java.lang.Boolean">true</value>
    </entry>
</util:map>

The handler will see the JAXB annotations and generate the required output.

处理程序将看到 JAXB 注释并生成所需的输出。

回答by jdevelop

Why not to try to use some concrete implementation of List? Or create your own marshaller which will try to understand what kind of list you're dealing with.

为什么不尝试使用 List 的一些具体实现呢?或者创建您自己的编组器,它会尝试了解您正在处理的列表类型。

http://fusesource.com/docs/esb/4.3.1/cxf_interceptors/CXFInterceptorIntro.html

http://fusesource.com/docs/esb/4.3.1/cxf_interceptors/CXFInterceptorIntro.html

http://cxf.apache.org/custom-cxf-transport.html

http://cxf.apache.org/custom-cxf-transport.html