首先定义实体类,因为这是要传递的对象,存放到map中.
package com.heppy1.cxf; public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
定义Map转换类,因为cxf默认是不支持传递复杂的传输数据类型,所以我们在传递的时候,需要将对象进行转换一下.
package com.heppy1.cxf; import java.util.Map; 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>> { @SuppressWarnings("unchecked") @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); } }
定义接口,这个呢就是我们要实现的接口,里面就一个getPerson方法,但是传递的对象却是Map类型的.
package com.heppy1.cxf; import java.util.Map; import javax.jws.WebService; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.heppy1.cxf.MapAdapter; import com.heppy1.cxf.Person;; @WebService public interface Heppy1Service { @XmlJavaTypeAdapter(MapAdapter.class) Map<String,Person> getPerson(@XmlJavaTypeAdapter(MapAdapter.class) Map<String,Person> person); }
编写实现类,编写一个实现方法,因只是个demo,也就直接写在实现方法中,直接返回了,如果应用与项目中,就根据自己项目的具体实现,需要什么样的数据,什么样的类型,自行修改,不过要记住,此处传递 Person要注意下,虽然我这里写的是Person,大家可以转成Object,这样就通用了,以后其他人可以拿你的代码直接用,这样也方便.
package com.heppy1.cxf; import java.util.HashMap; import java.util.Map; import javax.jws.WebService; @WebService(endpointInterface="com.heppy1.cxf.Heppy1Service",serviceName="heppy1.com") public class Heppy1ServiceImpl implements Heppy1Service{ private final Map<String,Person> objMap = new HashMap<String, Person>(); @Override public Map<String, Person> getPerson(Map<String, Person> person) { Person objPeron1 = new Person(); objPeron1.setName("张三"); objMap.put("obj1",objPeron1); Person objPeron2 = new Person(); objPeron2.setName("李四"); objMap.put("obj2",objPeron2); Person objPeron3 = new Person(); objPeron3.setName("王五"); objMap.put("obj3",objPeron2); return objMap; } }
package com.heppy1.cxf; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class Server { public static void main(String args[]) throws InterruptedException{ JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); factoryBean.setAddress("http://localhost:9000/service");//发布地址 Heppy1Service ds = new Heppy1ServiceImpl(); factoryBean.setServiceBean(ds); factoryBean.create(); } }
package com.heppy1.cxf; import java.util.HashMap; import java.util.Map; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class Client { public static void main(String[] args) { JaxWsProxyFactoryBean objWsProxyFactoryBean = new JaxWsProxyFactoryBean(); objWsProxyFactoryBean.setAddress("http://localhost:9000/service"); objWsProxyFactoryBean.setServiceClass(Heppy1Service.class); Heppy1Service objHelloService = (Heppy1Service)objWsProxyFactoryBean.create(); Map<String,Person> objMap = objHelloService.getPerson(new HashMap<String, Person>()); for(int i=1;i<objMap.size()+1;i++){ Person person = objMap.get("obj"+i); System.out.println(person.getName());; } } }
当我执行完Client,也就打印出了以下的数据.
这是我工程中引用的jar包
大家有不懂的可以来群里咨询,群里有很多老油条,而且这个源码我也放到群共享里面了,大家自己下载,欢迎大家来群里哦~~
QQ群: 100162042