服务接口
@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是需要转换的对象。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。