from: http://blog.csdn.net/wlsyn/article/details/8756068 CXF形参、返回值 1. 当形参和返回值的类型是String、基本数据类型是,CXF肯定可以轻松处理 2.当形参和返回值的类型是javabean式的复合类(就是
http://blog.csdn.net/wlsyn/article/details/8756068
CXF形参、返回值 1. 当形参和返回值的类型是String、基本数据类型是,CXF肯定可以轻松处理 2.当形参和返回值的类型是javabean式的复合类(就是普通的POJO实体类)、List集合、数组等复杂类型时, CXF也可以很好处理。 3.还有一些像Map、非javabean式的复合类,CXF是处理不了的
如果遇到系统无法自动处理的类型,就需要程序员自行处理,方法是提供一个转化器,该转化器负责把CXF不能处理的类型,转化为CXF能够处理的类型,具体过程如下: (1) 使用注解 @XmlJavaTypeAdapter(java自身的注解,可在jdkAPI文档中查到)修饰CXF无法自动处理的类型,使用该Annotation时,通过value属性指定一个转换器(自己定义)。 @XmlJavaTypeAdapter (value="MyXmlAdapter.class")
(2) 实现自己定义的转化器,实现转化器时,需要开发一个CXF能够处理的类型。
1. 注解 @XmlJavaTypeAdapter标识返回值为Map的接口
[html] view plain copy
- @WebService
- public interface HelloWorld {
- @XmlJavaTypeAdapter((XmlMapAdapter.class)) Map<String, String> getSpace(String deviceIp);
- }
- @Component("hello")
- @WebService(endpointInterface = "demo.spring.service.HelloWorld")
- public class HelloWorldImpl implements HelloWorld {
- public Map<String, String> getSpace(String deviceIp) {
- // TODO Auto-generated method stub
- HashMap<String, String> test = new HashMap<String,String>();
- 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<MyStringMap, Map<String, String>> {
- @Override
- public Map<String, String> unmarshal(MyStringMap v) throws Exception {
- // TODO Auto-generated method stub
- Map<String, String> result = new HashMap<String, String>();
- for (Entry entry : v.getEntries()) {
- result.put(entry.getKey(), entry.getValue());
- }
- return result;
- }
- @Override
- public MyStringMap marshal(Map<String, String> v) throws Exception {
- // TODO Auto-generated method stub
- MyStringMap msm = new MyStringMap();
- List<Entry> eList = new ArrayList<Entry>();
- 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<Entry> entries;
- /**
- * @return entries
- */
- public List<Entry> getEntries() {
- return entries;
- }
- /**
- * @param entries the entries to set
- */
- public void setEntries(List<Entry> 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 = value;
- }
- }
- }
avax.xml.bind.annotation.adapters
Class XmlAdapter<ValueType,BoundType>
- java.lang.Object
- javax.xml.bind.annotation.adapters.XmlAdapter<ValueType,BoundType>
-
- 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<Entry> 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