• CXF学习(4) 处理无法自动转换的复合数据类型


    只贴出服务端代码

    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 暴露成功");
        }
    }
  • 相关阅读:
    css3新单位vw、vh、vmin、vmax的使用介绍
    vue中的css作用域、vue中的scoped坑点
    vue组件中的样式属性:scoped,解决在父组件中无法修改子组件样式问题
    修改elementUI组件样式无效的问题研究
    Javascript里面的时间处理:将时间戳或时间对象转成字符串格式
    详解vue父组件传递props异步数据到子组件的问题
    vue父组件异步传递prop到子组件echarts画图问题踩坑总结
    父组件中调用子组件的方法和属性
    Echarts使用dataset数据集管理数据
    Echarts的legend改变图例图标为自定义图片
  • 原文地址:https://www.cnblogs.com/heben/p/5408343.html
Copyright © 2020-2023  润新知