Apache CXF解析Map,HashMap

项目中WebService框架用的是Apache CXF,但是在使用中发现Apache CXF不支持解析Map和HashMap,而且SOAP报文(XML)和JavaBean的转化是通过JAXB实现的,没办法,自己写了个Map到XML的适配器,来实现两者的转化。


import java.util.HashMap;
import java.util.Map;


import javax.xml.bind.annotation.adapters.XmlAdapter;


/**
 * simple introduction Map适配器,完成Java中map与XML中对应节点的转换
 * 
 * <p>
 * detailed comment
 * @author zWX184091 2013-8-15
 * @see
 * @since 1.0
 */
public class MapAdapter extends XmlAdapter<MapConvertor, HashMap<String, String>>
{


    /**
     * XML to JAVA
     * 
     * @param map
     * @return HashMap<String, String>
     * @throws Exception
     */
    @Override
    public HashMap<String, String> unmarshal(MapConvertor map) throws Exception
    {
        // TODO Auto-generated method stub
        HashMap<String, String> result = new HashMap<String, String>();


        // 遍历MapConvertor,将XML节点内容写入JavaBean Map对象
        for (MapConvertor.MapEntry e : map.getEntry())
        {
            result.put(e.getKey(), e.getValue());
        }
        return result;
    }


    /**
     * JAVA to XML
     * 
     * @param map
     * @return MapConvertor
     * @throws Exception
     */
    @Override
    public MapConvertor marshal(HashMap<String, String> map) throws Exception
    {


        // 创建MapConvertor对象,盛放XML节点内容
        MapConvertor convertor = new MapConvertor();


        // 遍历map,将JavaBean中数据写入XML节点
        for (Map.Entry<String, String> entry : map.entrySet())
        {
            // 创建空的MapEntry对象(该mapEntry应该放在循环内,防止重复使用同一个java对象引用)
            MapConvertor.MapEntry mapEntry = new MapConvertor.MapEntry();


            mapEntry.setKey(entry.getKey());
            mapEntry.setValue(entry.getValue());


            convertor.addEntry(mapEntry);
        }
        return convertor;
    }
}




import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;


import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * simple introduction Map转换器
 * 
 * <p>
 * detailed comment
 * @author zWX184091 2013-7-31
 * @see
 * @since 1.0
 */
@XmlType(name = "MapConvertor")
@XmlAccessorType(XmlAccessType.FIELD)
public class MapConvertor
{


    // SOAP报文结构是一个Map的List
    private List<MapEntry> entry = new ArrayList<MapEntry>();


    public void addEntry(MapEntry entry)
    {
        this.entry.add(entry);
    }


    public List<MapEntry> getEntry()
    {
        return entry;
    }


    public void setEntry(List<MapEntry> entry)
    {
        this.entry = entry;
    }


    public static class MapEntry
    {
        private String key;


        private String value;


        public MapEntry()
        {
            super();
        }


        public MapEntry(String key, String value)
        {
            super();
            this.key = key;
            this.value = value;
        }


        public MapEntry(Entry<String, String> entry)
        {
            super();
            this.key = entry.getKey();
            this.value = entry.getValue();
        }


        public String getKey()
        {
            return key;
        }


        public String getValue()
        {
            return value;
        }


        public void setKey(String key)
        {
            this.key = key;
        }


        public void setValue(String value)
        {
            this.value = value;
        }
    }
}




public class SigParam
{
    // SOAP报文结构是一个Map的集合(List)
    private List<HashMap<String, String>> entry;


    public SigParam(List<HashMap<String, String>> entry)
    {
        super();
        this.entry = entry;
    }


    public SigParam()
    {
        super();
    }


    @XmlElement(name = "string2stringMap")
    @XmlJavaTypeAdapter(MapAdapter.class)
    public List<HashMap<String, String>> getEntry()
    {
        return entry;
    }


    public void setEntry(List<HashMap<String, String>> entry)
    {
        this.entry = entry;
    }
}



SOAP报文内容:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.ws.userinterface.sa.security.com/">
   <soapenv:Header>
      <userId>WHITE_GROUP_ADD_001</userId>
      <password>000</password>
   </soapenv:Header>
   <soapenv:Body>
      <ser:execute>
         <arg0>
            <records>
               <string2stringMap>
                  <entry>
                     <key>groupName</key>
                     <value>Hello_ggood</value>
                  </entry>
                  <entry>
                     <key>adName</key>
                     <value>最后一次测试</value>
                  </entry>
                  <entry>
                     <key>time</key>
                     <value>0909</value>
                  </entry>
               </string2stringMap>
            </records>
            <taskCode>WHITE_GROUP_ADD_001</taskCode>
         </arg0>
      </ser:execute>
   </soapenv:Body>
</soapenv:Envelope>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值