Cxf 复杂数据类型(Map)
发布时间:2020-12-17 00:40:56 所属栏目:安全 来源:网络整理
导读:?cxf本身不支持复杂数据类型。需要提供一个转换类。 ??? 服务接口 @WebServicepublic interface IHelloService {@WebMethodString sayHello(String name);@WebResult(name="listPerson") ListPerson getPersionByName(@WebParam(name="names")String[] names
?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);
该类必须是XmlAdapter<ValueType,BoundType> 其中ValueType是转换后Cxf能够支持的对象。 BoundType是需要转换的对象。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |