webservice学习之处理Map等CXF无法自动转化的类型
转自:http://blog.csdn.net/wlsyn/article/details/8756068
【webservice学习之处理Map等CXF无法自动转化的类型】
CXF形参、返回值 1. 当形参和返回值的类型是String、基本数据类型是,CXF肯定可以轻松处理 2.当形参和返回值的类型是javabean式的复合类(就是普通的POJO实体类)、List集合、数组等复杂类型时, CXF也可以很好处理。 3.还有一些像Map、非javabean式的复合类,CXF是处理不了的
如果遇到系统无法自动处理的类型,就需要程序员自行处理,方法是提供一个转化器,该转化器负责把CXF不能处理的类型,转化为CXF能够处理的类型,具体过程如下: (1) 使用注解 @XmlJavaTypeAdapter(java自身的注解,可在jdkAPI文档中查到)修饰CXF无法自动处理的类型,使用该Annotation时,通过value属性指定一个转换器(自己定义)。 @XmlJavaTypeAdapter (value="https://www.it610.com/article/MyXmlAdapter.class")
(2) 实现自己定义的转化器,实现转化器时,需要开发一个CXF能够处理的类型。
1. 注解@XmlJavaTypeAdapter标识返回值为Map的接口
[html]view plain copy
- @WebService
- public interface HelloWorld {
- @XmlJavaTypeAdapter((XmlMapAdapter.class)) Map
getSpace(String deviceIp); - }
- @Component("hello")
- @WebService(endpointInterface = "demo.spring.service.HelloWorld")
- public class HelloWorldImpl implements HelloWorld {
- public Map
getSpace(String deviceIp) { - // TODO Auto-generated method stub
- HashMap
test = new HashMap (); - test.put("test","10.5");
- test.put("ip", deviceIp);
- System.out.println("deviceIp: " + deviceIp);
- return test;
- }
- }
2.定义自行创建的XmlMapAdapter类型 [html]view plain copy
- public class XmlMapAdapter extends XmlAdapter
> { - @Override
- public Map
unmarshal(MyStringMap v) throws Exception { - // TODO Auto-generated method stub
- Map
result = new HashMap (); - for (Entry entry : v.getEntries()) {
- result.put(entry.getKey(), entry.getValue());
- }
- return result;
- }
- @Override
- public MyStringMap marshal(Map
v) throws Exception { - // TODO Auto-generated method stub
- MyStringMap msm = new MyStringMap();
- List
eList = new ArrayList (); - for(String key : v.keySet()) {
- Entry entry = new Entry();
- entry.setKey(key);
- entry.setValue(v.get(key));
- eList.add(entry);
- }
- msm.setEntries(eList);
- return msm;
- }
- }
- public class MyStringMap {
- private List
entries; - /**
- * @return entries
- */
- public List
getEntries() { - return entries;
- }
- /**
- * @param entries the entries to set
- */
- public void setEntries(List
entries) { - this.entries = entries;
- }
- public static class Entry {
- private String key;
- private String value;
- /**
- * @return key
- */
- public String getKey() {
- return key;
- }
- /**
- * @param key the key to set
- */
- public void setKey(String key) {
- this.key = key;
- }
- /**
- * @return value
- */
- public String getValue() {
- return value;
- }
- /**
- * @param value the value to set
- */
- public void setValue(String value) {
- this.value = https://www.it610.com/article/value;
- }
- }
- }
avax.xml.bind.annotation.adapters Class XmlAdapter
- java.lang.Object
- javax.xml.bind.annotation.adapters.XmlAdapter
-
- Type Parameters:
-
BoundType
- The type that JAXB doesn't know how to handle. An adapter is written to allow this type to be used as an in-memory representation through the ValueType. -
ValueType
- The type that JAXB knows how to handle out of the box.
3.部署项目到tomcat中,启动,如能访问到WSDL文件,WSDL发布成功。
4.使用命令生成客户端,具体方法见 博文。
5.测试客户端: [html]view plain copy
- public static void main(String []args) {
- HelloWorldImplService service = new HelloWorldImplService();
- HelloWorld hw = service.getHelloWorldImplPort();
- MyStringMap msm = hw.getSpace("");
- List
entries = msm.getEntries(); - for (Entry e : entries) {
- System.out.println("key: " + e.getKey() + " " + "value: " + e.getValue());
- }
- }
结果如下: [html]view plain copy
- 2013-4-3 15:56:19 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
- 信息: Creating Service {http://service.spring.demo/}HelloWorldImplService from WSDL: http://192.168.1.133:8088/CXFUseCase/services/helloWorld?wsdl
- key: test value: 10.5
- key: ip value: 192.168.3.51
至此,转化操作完成。
推荐阅读
- 慢慢的美丽
- 开学第一天(下)
- 奔向你的城市
- 学无止境,人生还很长
- 由浅入深理解AOP
- “成长”读书社群招募
- 继续努力,自主学习家庭Day135(20181015)
- python学习之|python学习之 实现QQ自动发送消息
- 每日一话(49)——一位清华教授在朋友圈给大学生的9条建议
- 小影写在2018九月开学季