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是需要转换的对象。