您的位置:首页 > 其它

Cxf 复杂数据类型(Map)

2014-06-23 14:52 281 查看
cxf本身不支持复杂数据类型。需要提供一个转换类。

服务接口

[java]
view plaincopy

@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<strong>(@XmlJavaTypeAdapter(MapAdapter.class)
Map<String,Person> person);
}
</strong>

实现类:

[html]
view plaincopy

@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;
}

}

服务发布应用:

[java]
view plaincopy

IHelloService objIHelloService = new HelloServiceImpl();

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

转换类

[java]
view plaincopy

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);
}

}

客户端:

[java]
view plaincopy

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是需要转换的对象。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: