CXF学习(4) 处理无法自动转换的复合数据类型
2016-04-19 15:18
405 查看
只贴出服务端代码
1.Service接口类
2.服务实现类
Cat类
StringCat
这是重点,适配器类
最后是运行类
1.Service接口类
package com.test.hello; import java.util.Map; import javax.jws.WebService; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.test.util.MineAdapter; @WebService public interface HelloWorld { // public void sayHello(); //cxf 不能处理 map 类型,于是我们采用MineAdapter.class进行处理 public @XmlJavaTypeAdapter(MineAdapter.class) Map<String,Cat> getMap(); }
2.服务实现类
package com.test.hello; import java.util.HashMap; import java.util.Map; import javax.jws.WebService; @WebService(endpointInterface="com.test.hello.HelloWorld",serviceName="HelloWorldWS") public class HelloWorldImpl implements HelloWorld{ private static Map<String,Cat> map; static { map = new HashMap<String, Cat>(); map.put("tomcat", new Cat()); map.put("garfield", new Cat()); } public Map<String,Cat> getMap() { return map; } }
Cat类
package com.test.hello; public class Cat { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
StringCat
package com.test.util; import java.util.ArrayList; import java.util.List; import com.test.hello.Cat; public class StringCat { public static class Entry { private String key; private Cat value; public Entry(String key, Cat value) { this.key=key; this.value=value; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public Cat getValue() { return value; } public void setValue(Cat value) { this.value = value; } } private List<Entry> entries = new ArrayList<StringCat.Entry>(); public List<Entry> getEntries() { return entries; } public void setEntries(List<Entry> entries) { this.entries = entries; } @Override public String toString() { StringBuilder str = null; for (Entry entry : entries) { str.append(entry.getKey() +" : "+entry.getValue()); } return str.toString(); } }
这是重点,适配器类
package com.test.util; import java.util.HashMap; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; import com.test.hello.Cat; import com.test.util.StringCat.Entry; //改转换器就负责完成StringCat与Map的相互转换 public class MineAdapter extends XmlAdapter<StringCat, Map<String,Cat>>{ @Override public Map unmarshal(StringCat strngCat) throws Exception { Map result = new HashMap(); for(Entry entry : strngCat.getEntries()){ result.put(entry.getKey(),entry.getValue()); } return result; } @Override public StringCat marshal(Map<String,Cat> map) throws Exception { StringCat stringCat = new StringCat(); for (String key : map.keySet()) { stringCat.getEntries().add(new Entry(key,map.get(key))); } return stringCat; } }
最后是运行类
package com.test.hello; import javax.xml.ws.Endpoint; public class ServerMain { public static void main(String[] args) { HelloWorld helloWorldImpl = new HelloWorldImpl(); Endpoint.publish("http://localhost:1234/ws", helloWorldImpl); System.out.println("webservice 暴露成功"); } }
相关文章推荐
- Apple Watch 2.0 数据通讯
- 对于“ip欺骗”的预防
- AndroidUI之三分钟教你实现效果简单大气的Dialog提示框
- ulimit设置--最大文件打开数
- Problem A
- Problem A
- 如何将txt中的大数据通过PL/SQL导入数据库
- 【OpenCV】OpenCV3的第九天——imgproc组件之特征检测
- JAVA 获取linux系统磁盘使用率
- leetcode111题 题解 翻译 C语言版 Python版
- Golang在windows下交叉编译linux程序
- Ubuntu14.04通过pyenv配置多python
- 图像预处理第4步:去离散杂点噪声
- C# HttpWebRequest类
- 程序员你为什么迷茫?
- x86 Registers and Architecture
- 微信开发‘该公共号暂时无法提供服务,请稍后再试’
- c++各容器使用心得
- 【OpenCV】OpenCV3的第八天——imgproc组件之结构分析
- 京东、阿里对比