Cxf 复杂数据类型(Map)

 cxf本身不支持复杂数据类型。需要提供一个转换类。

    服务接口

@WebService
public interface IHelloService {

	@WebMethod
	String sayHello(String name);
	
	@WebResult(name="listPerson")
         List<Person> getPersionByName(@WebParam(name="names")String[] names);
                                                                                                                                             @XmlJavaTypeAdapter(MapAdapter.class)
         Map<String,Person> getPerson(@XmlJavaTypeAdapter(MapAdapter.class)
         Map<String,Person> person);
}


实现类:

 

@WebService(endpointInterface="com.skydream.cxf.IHelloService",serviceName="helloService")
public class HelloServiceImpl implements IHelloService 
{
	private final List<Person> lstPerson = new ArrayList<Person>();
	
	private final Map<String,Person> objMap = new HashMap<String, Person>();

	public HelloServiceImpl()
	{
		Person objPeron1 = new Person();
		objPeron1.setId("001");
		objPeron1.setName("skydream");
		objPeron1.setAge(25);
		lstPerson.add(objPeron1);
		objMap.put(objPeron1.getId(),objPeron1);
		
		Person objPeron2 = new Person();
		objPeron2.setId("002");
		objPeron2.setName("xuhaibo");
		objPeron2.setAge(24);
		lstPerson.add(objPeron2);
		objMap.put(objPeron2.getId(),objPeron2);
		
		Person objPeron3 = new Person();
		objPeron3.setId("003");
		objPeron3.setName("xufuyu");
		objPeron3.setAge(25);
		lstPerson.add(objPeron3);
		objMap.put(objPeron2.getId(),objPeron2);
	}
	
	@Override
	public String sayHello(String name) 
	{
		// TODO Auto-generated method stub
		return "Hello "+name+" ;Now time is "+new Date();
	}

	@Override
	public List<Person> getPersionByName(String[] names) {
		
		List<Person> persons = new ArrayList<Person>();
		for(Person objPerson : lstPerson)
		{
			if(java.util.Arrays.asList(names).contains(objPerson.getName()))
			{
				persons.add(objPerson);
			}
		}
		return persons;
	}

	@Override
	public Map<String, Person> getPerson(Map<String, Person> person) {
		person = objMap;
		return person;
	}

}


 

服务发布应用:

 IHelloService objIHelloService = new HelloServiceImpl();
		
		CxfPublish objCxfPublish = new CxfPublish();
		objCxfPublish.publishCxf("http://127.0.0.1/helloService", objIHelloService);
		System.out.println("publish success!");


 

转换类

 

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

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class MapAdapter extends XmlAdapter<String, Map<String,Person>> {

	@Override
	public Map<String, Person> unmarshal(String v) throws Exception {
		XStream objXStream = new XStream(new DomDriver());
		return (Map<String, Person>) objXStream.fromXML(v);
	}

	@Override
	public String marshal(Map<String, Person> v) throws Exception {
		XStream objXStream = new XStream();
		return objXStream.toXML(v);
	}

}

客户端:

  

JaxWsProxyFactoryBean objWsProxyFactoryBean = new JaxWsProxyFactoryBean();
		objWsProxyFactoryBean.setAddress("http://127.0.0.1/helloService");
		objWsProxyFactoryBean.setServiceClass(IHelloService.class);
	    IHelloService objHelloService =	(IHelloService)objWsProxyFactoryBean.create();
	    
	    String strResult = objHelloService.sayHello("skydream");
	    System.out.println("client sahHello result :"+strResult);
	    
	    List<Person> lstPerson = objHelloService.getPersionByName(new String[]{"skydream","xuhaibo","god"});
	    System.out.println("This is the getPersonByName result :" + lstPerson);
	    
	    Map<String,Person> objMap = objHelloService.getPerson(new HashMap<String, Person>());
	    System.out.println(objMap);


说明:转换类可以自己实现,本文使用了xstream.jar提供的XStream实现对象和String类型。
@XmlJavaTypeAdapter(MapAdapter.class)是标明该处需要进行转换,转换过的工具方法是MapAdapter.

该类必须是XmlAdapter<ValueType,BoundType>

其中ValueType是转换后Cxf能够支持的对象。

BoundType是需要转换的对象。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值