WebService2

Posted xxbbtt

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebService2相关的知识,希望对你有一定的参考价值。

一、传递复杂对象(list、map、自定义对象)

  在使用WebService服务的时候经常需要传递参数,我们不可能只使用基本类型,所以也应该支持复杂对象。先建立一个复杂的类:

  类的代码比较多也比较简单,这个类需要实现Serializable接口,也就是说必须得实现可序列化才行。因为代码比较多我就直接折叠了,这么多字段的原因是想构建一个相当复杂的类。。。。

public class Address implements Serializable {
    private static final long UID = 3222222L;

    private Integer identifier;

    //地址
    private String address;

    //城市
    private String city;

    //省份
    private String province;

    //国家
    private String country;

    //邮编
    private String postalCode;

    private String[] array;

    private List<Integer> list;

    private boolean isExist;

    public Address(){
        list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
    }

    public Integer getIdentifier() {
        return identifier;
    }

    public void setIdentifier(Integer identifier) {
        this.identifier = identifier;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String[] getArray() {
        return array;
    }

    public void setArray(String[] array) {
        this.array = array;
    }

    public List<Integer> getList() {
        return list;
    }

    public void setList(List<Integer> list) {
        this.list = list;
    }

    public boolean isExist() {
        return isExist;
    }

    public void setExist(boolean exist) {
        isExist = exist;
    }
}
View Code

  然后就是建立具体的WebService类,这个服务的功能注释中写得很清楚

public class AddressManager {

    //将返回一个预先建立好的Address对象,测试返回复杂对象
    public Address getaddress(){
        Address address = new Address();
        address.setIdentifier(1);
        address.setAddress("xx");
        address.setCity("yy");
        address.setProvince("zz");
        address.setCountry("CN");
        address.setPostalCode("10086");
        address.setExist(false);
        address.setArray(new String[]{"1","2","3"});

        return address;
    }
    
    //返回传递上来的Address对象,不做修改,测试上传复杂对象
    public Address setaddress(Address address){
        return address;
    }

    //将返回一个预先建立好的list,测试返回list
    public List<Address> getaddressList() {
        List<Address> returnList = new ArrayList<Address>();

        Address address = new Address();
        address.setIdentifier(1);
        address.setAddress("xx");
        address.setCity("yy");
        address.setProvince("zz");
        address.setCountry("CN");
        address.setPostalCode("10086");
        address.setExist(false);
        address.setArray(new String[]{"1","2","3"});

        returnList.add(address);

        address = new Address();
        address.setIdentifier(2);
        address.setAddress("xx2");
        address.setCity("yy2");
        address.setProvince("zz2");
        address.setCountry("CN2");
        address.setPostalCode("100862");
        address.setExist(true);
        address.setArray(new String[]{"12","22","32"});

        returnList.add(address);

        return returnList;
    }

    //返回传递上来的list,不做修改,测试上传list
    public List<Address> setAddressList(List<Address> list){
        return list;
    }

    //将返回一个预先建立好的map,测试返回map
    public Map<Integer, Address> getAddressMap(){
        Map<Integer,Address> returnMap = new HashMap<Integer,Address>();

        Address address = new Address();
        address.setIdentifier(1);
        address.setAddress("xx");
        address.setCity("yy");
        address.setProvince("zz");
        address.setCountry("CN");
        address.setPostalCode("10086");
        address.setExist(false);
        address.setArray(new String[]{"1","2","3"});

        returnMap.put(address.getIdentifier(),address);

        address = new Address();
        address.setIdentifier(2);
        address.setAddress("xx2");
        address.setCity("yy2");
        address.setProvince("zz2");
        address.setCountry("CN2");
        address.setPostalCode("100862");
        address.setExist(true);
        address.setArray(new String[]{"12","22","32"});

        returnMap.put(address.getIdentifier(),address);

        return returnMap;

    }

    //返回传递上来的map,不做修改,测试上传map
    public Map<Integer,Address> setAddressMap(Map<Integer,Address> map){
        return map;
    }

}

  然后就是发布这个服务,在server-config.wsdd添加以下XML代码:

  <service name="AddressManager" provider="java:RPC">
    <parameter name="className" value="example.AddressManager"/>
    <parameter name="allowedMethods" value="*"/>
    <beanMapping qname="myNSD:Address"
                 xmlns:myNSD="urn:AddressManager"
                 languageSpecificType="java:model.Address" />
  </service>

   和之前不同的就是多了<beabMapping>这个节点。qname 表示 XML 规范中定义的限定名称,他由名称空间 URI、本地部分和前缀组成。除了本地部分其他都不是必须的另外QName是不可变的。xmlns后面的myNS是必须的。具体根据前面所指定的 qname来决定。之后可以随意命名一个namespace。其实我可以任意写几个可以用的就可以大概看出规律了:

    <beanMapping qname="myNSD:Address1"
                 xmlns:myNSD="urn:AddressManager1"
                 languageSpecificType="java:model.Address" />
    <beanMapping qname="NSD:Address"
                 xmlns:NSD="urn:AddressManager"
                 languageSpecificType="java:model.Address" />

  然后我在用特殊符号总结一下,该怎么编写这个节点

    <beanMapping qname="***:+++"
                 xmlns:***="!!!:---"
                 languageSpecificType="java:model.Address" />

  ***处是两个一一对应的,就是说任意编写只是这两个要一样,+++、---、!!!这三处任意填写。。这里的qname客户端将会用到。

  最后的languageSpecificType指定的是你传递的对象类型。

  到此点击运行,在浏览器中输入http://localhost:8080/test/services可以看到,AddressManager服务已经被发布还可以看到可以被调用的方法:

  

 

  然后是客户端的代码:

  首先是获取一个Adress对象的方法,具体流程注释中写得很清楚,要注意的地方有生成Qname时第一个参数名字空间URI,第二个参数本地部分,注意这两部分在server-config.wsdd文件中标签beanMapping配置:

public static void getServerAddress(){
        try {
            String url =
                    "http://localhost:8080/test/services/AddressManager?wsdl";
            //生成服务对象Service
            Service service = new Service();
            Call call = (Call) service.createCall();

            //生成QName,两个参数与服务端的发布时的XML设定有关
            QName qn = new QName("urn:AddressManager","Address");

            //注册参数
            call.registerTypeMapping(Address.class, qn,
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(Address.class, qn),
                    new org.apache.axis.encoding.ser.BeanDeserializerFactory(Address.class, qn));

            //设置Endpoint地址
            call.setTargetEndpointAddress(new java.net.URL(url));
            //绑定请求方法名称
            call.setOperationName(new QName(url, "getaddress"));

            //设置返回类型
            call.setReturnClass(Address.class);

            Object[] sss = null;

            //通过call.invoke 调用服务,获取返回值
            Address address = (Address) call.invoke(sss);

            System.out.println("Address: ");

            System.out.println(" id号: " + address.getIdentifier()
                    + " address: " + address.getAddress()
                    + " city: " + address.getCity()
                    + " province: " + address.getProvince()
                    + " country: " + address.getCountry()
                    + " postalCode: " + address.getPostalCode()
                    + " array: " + address.getArray()[0]
                    + " list: " + address.getList()
                    + " isExist " + address.isExist());

        }catch (ServiceException e){
            System.out.println("ServiceException");
            e.printStackTrace();
        }catch (RemoteException e){
            System.out.println("RemoteException");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException");
            e.printStackTrace();
        }
    }

  运行这个方法可以看到输出,已经得到了这个Address对象:

  

  然后是上传一个Address对象在客户端的代码,比刚才多一个call.addParameter("address", qn , ParameterMode.IN);用于注册参数,如果上传的是String类型就不需要这条语句。

    public static void setServerAddress(){
        Address address = new Address();
        address.setIdentifier(1);
        address.setAddress("xx");
        address.setCity("yy");
        address.setProvince("zz");
        address.setCountry("CN");
        address.setPostalCode("10086");
        address.setExist(false);
        address.setArray(new String[]{"1","2","3"});

        try {
            String url =
                    "http://localhost:8080/test/services/AddressManager?wsdl";
            //生成服务对象Service
            Service service = new Service();
            Call call = (Call) service.createCall();

            //生成QName,两个参数与服务端的发布时的XML设定有关
            QName qn = new QName("urn:AddressManager","Address");

            //注册参数
            call.registerTypeMapping(Address.class, qn,
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(Address.class, qn),
                    new org.apache.axis.encoding.ser.BeanDeserializerFactory(Address.class, qn));

            //设置Endpoint地址
            call.setTargetEndpointAddress(new java.net.URL(url));
            //绑定请求方法名称
            call.setOperationName(new QName(url, "setaddress"));

            //设置返回类型
            call.setReturnClass(Address.class);

            //定义一个参数类型,如果是String类型的参数可以不需要这句话
            call.addParameter("address", qn , ParameterMode.IN);

            //通过call.invoke 调用服务,获取返回值
            address = (Address) call.invoke(new Object[]{address});

            System.out.println("Address: ");

            System.out.println(" id号: " + address.getIdentifier()
                    + " address: " + address.getAddress()
                    + " city: " + address.getCity()
                    + " province: " + address.getProvince()
                    + " country: " + address.getCountry()
                    + " postalCode: " + address.getPostalCode()
                    + " array: " + address.getArray()[0]
                    + " list: " + address.getList()
                    + " isExist " + address.isExist());

        }catch (ServiceException e){
            System.out.println("ServiceException");
            e.printStackTrace();
        }catch (RemoteException e){
            System.out.println("RemoteException");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException");
            e.printStackTrace();
        }
    }

  输出是:

  

  然后下面的代码是全部的代码,包括list和map:

public class ClientAddress {
    public static void getServerList(){
        try {
            String url =
                    "http://localhost:8080/test/services/AddressManager?wsdl";
            //生成服务对象Service
            Service service = new Service();
            Call call = (Call) service.createCall();

            QName qn = new QName("urn:AddressManager","Address");

            call.registerTypeMapping(Address.class, qn,
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(Address.class, qn),
                    new org.apache.axis.encoding.ser.BeanDeserializerFactory(Address.class, qn));

            //设置Endpoint地址
            call.setTargetEndpointAddress(new java.net.URL(url));
            //绑定请求方法名称
            call.setOperationName(new QName(url, "getaddressList"));

            call.setReturnClass(ArrayList.class);

            Object[] sss = null;

            //通过call.invoke 调用服务,获取返回值
            List<Address> list = (ArrayList) call.invoke(sss);

            System.out.println("List size: " + list.size());

            for(Iterator<Address> iter = list.iterator(); iter.hasNext();){
                Address address = iter.next();
                System.out.println("id号: " + address.getIdentifier()
                        + " address: " + address.getAddress()
                        + " city: " + address.getCity()
                        + " province: " + address.getProvince()
                        + " country: " + address.getCountry()
                        + " postalCode: " + address.getPostalCode()
                        + " array: " + address.getArray()[0]
                        + " list: " + address.getList()
                        + " isExist " + address.isExist());
            }


        }catch (ServiceException e){
            System.out.println("ServiceException");
            e.printStackTrace();
        }catch (RemoteException e){
            System.out.println("RemoteException");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException");
            e.printStackTrace();
        }
    }

    public static void getServerMap(){
        try {
            String url =
                    "http://localhost:8080/test/services/AddressManager?wsdl";
            //生成服务对象Service
            Service service = new Service();
            Call call = (Call) service.createCall();

            QName qn = new QName("urn:AddressManager","Address");

            call.registerTypeMapping(Address.class, qn,
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(Address.class, qn),
                    new org.apache.axis.encoding.ser.BeanDeserializerFactory(Address.class, qn));

            //设置Endpoint地址
            call.setTargetEndpointAddress(new java.net.URL(url));
            //绑定请求方法名称
            call.setOperationName(new QName(url, "getAddressMap"));

            call.setReturnClass(HashMap.class);

            Object[] sss = null;

            //通过call.invoke 调用服务,获取返回值
            Map<Integer,Address> map = (Map) call.invoke(sss);

            System.out.println("map size: " + map.size());

            for(Iterator<Integer> iter = map.keySet().iterator(); iter.hasNext();){
                Integer key = iter.next();
                Address address = map.get(key);
                System.out.println(" id号: " + address.getIdentifier()
                        + " address: " + address.getAddress()
                        + " city: " + address.getCity()
                        + " province: " + address.getProvince()
                        + " country: " + address.getCountry()
                        + " postalCode: " + address.getPostalCode()
                        + " array: " + address.getArray()[0]
                        + " list: " + address.getList()
                        + " isExist " + address.isExist());
            }


        }catch (ServiceException e){
            System.out.println("ServiceException");
            e.printStackTrace();
        }catch (RemoteException e){
            System.out.println("RemoteException");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException");
            e.printStackTrace();
        }
    }

    public static void setServerList(){
        List<Address> list = new ArrayList<Address>();

        Address address = new Address();
        address.setIdentifier(3);
        address.setAddress("xx3");
        address.setCity("yy3");
        address.setProvince("zz3");
        address.setCountry("CN3");
        address.setPostalCode("100863");
        address.setExist(false);
        address.setArray(new String[]{"13","23","33"});

        list.add(address);

        address = new Address();
        address.setIdentifier(4);
        address.setAddress("xx4");
        address.setCity("yy4");
        address.setProvince("zz4");
        address.setCountry("CN4");
        address.setPostalCode("100864");
        address.setExist(true);
        address.setArray(new String[]{"14","24","34"});

        list.add(address);

        try {
            String url =
                    "http://localhost:8080/test/services/AddressManager?wsdl";
            //生成服务对象Service
            Service service = new Service();
            Call call = (Call) service.createCall();

            QName qn = new QName("urn:AddressManager","Address");

            call.registerTypeMapping(Address.class, qn,
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(Address.class, qn),
                    new org.apache.axis.encoding.ser.BeanDeserializerFactory(Address.class, qn));

            //设置Endpoint地址
            call.setTargetEndpointAddress(new java.net.URL(url));
            //绑定请求方法名称
            call.setOperationName(new QName(url, "setAddressList"));

            call.setReturnClass(ArrayList.class);

            call.addParameter("list", qn , ParameterMode.IN);

            //通过call.invoke 调用服务,获取返回值
            list = (ArrayList) call.invoke(new Object[]{list});

            System.out.println("list size: " + list.size());

            for(Iterator<Address> iter = list.iterator(); iter.hasNext();){
                address = iter.next();
                System.out.println("id号: " + address.getIdentifier()
                        + " address: " + address.getAddress()
                        + " city: " + address.getCity()
                        + " province: " + address.getProvince()
                        + " country: " + address.getCountry()
                        + " postalCode: " + address.getPostalCode()
                        + " array: " + address.getArray()[0]
                        + " list: " + address.getList()
                        + " isExist " + address.isExist());
            }
        }catch (ServiceException e){
            System.out.println("ServiceException");
            e.printStackTrace();
        }catch (RemoteException e){
            System.out.println("RemoteException");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException");
            e.printStackTrace();
        }
    }

    public static void setServerMap(){
        Map<Integer,Address> map = new HashMap<Integer,Address>();

        Address address = new Address();
        address.setIdentifier(3);
        address.setAddress("xx3");
        address.setCity("yy3");
        address.setProvince("zz3");
        address.setCountry("CN3");
        address.setPostalCode("100863");
        address.setExist(false);
        address.setArray(new String[]{"13","23","33"});

        map.put(address.getIdentifier(),address);

        address = new Address();
        address.setIdentifier(4);
        address.setAddress("xx4");
        address.setCity("yy4");
        address.setProvince("zz4");
        address.setCountry("CN4");
        address.setPostalCode("100864");
        address.setExist(true);
        address.setArray(new String[]{"14","24","34"});

        map.put(address.getIdentifier(),address);

        try {
            String url =
                    "http://localhost:8080/test/services/AddressManager?wsdl";
            //生成服务对象Service
            Service service = new Service();
            Call call = (Call) service.createCall();

            QName qn = new QName("urn:AddressManager","Address");

            call.registerTypeMapping(Address.class, qn,
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(Address.class, qn),
                    new org.apache.axis.encoding.ser.BeanDeserializerFactory(Address.class, qn));

            //设置Endpoint地址
            call.setTargetEndpointAddress(new java.net.URL(url));
            //绑定请求方法名称
            call.setOperationName(new QName(url, "setAddressMap"));

            call.setReturnClass(HashMap.class);

            call.addParameter("map", qn , ParameterMode.IN);

            //通过call.invoke 调用服务,获取返回值
            map = (Map) call.invoke(new Object[]{map});

            System.out.println("map size: " + map.size());

            for(Iterator<Integer> iter = map.keySet().iterator(); iter.hasNext();){
                Integer key = iter.next();
                address = map.get(key);
                System.out.println(" id号: " + address.getIdentifier()
                        + " address: " + address.getAddress()
                        + " city: " + address.getCity()
                        + " province: " + address.getProvince()
                        + " country: " + address.getCountry()
                        + " postalCode: " + address.getPostalCode()
                        + " array: " + address.getArray()[0]
                        + " list: " + address.getList()
                        + " isExist " + address.isExist());
            }
        }catch (ServiceException e){
            System.out.println("ServiceException");
            e.printStackTrace();
        }catch (RemoteException e){
            System.out.println("RemoteException");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException");
            e.printStackTrace();
        }
    }

    public static void getServerAddress(){
        try {
            String url =
                    "http://localhost:8080/test/services/AddressManager?wsdl";
            //生成服务对象Service
            Service service = new Service();
            Call call = (Call) service.createCall();

            //生成QName,两个参数与服务端的发布时的XML设定有关
            QName qn = new QName("urn:AddressManager","Address");

            //注册参数
            call.registerTypeMapping(Address.class, qn,
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(Address.class, qn),
                    new org.apache.axis.encoding.ser.BeanDeserializerFactory(Address.class, qn));

            //设置Endpoint地址
            call.setTargetEndpointAddress(new java.net.URL(url));
            //绑定请求方法名称
            call.setOperationName(new QName(url, "getaddress"));

            //设置返回类型
            call.setReturnClass(Address.class);

            Object[] sss = null;

            //通过call.invoke 调用服务,获取返回值
            Address address = (Address) call.invoke(sss);

            System.out.println("Address: ");

            System.out.println(" id号: " + address.getIdentifier()
                    + " address: " + address.getAddress()
                    + " city: " + address.getCity()
                    + " province: " + address.getProvince()
                    + " country: " + address.getCountry()
                    + " postalCode: " + address.getPostalCode()
                    + " array: " + address.getArray()[0]
                    + " list: " + address.getList()
                    + " isExist " + address.isExist());

        }catch (ServiceException e){
            System.out.println("ServiceException");
            e.printStackTrace();
        }catch (RemoteException e){
            System.out.println("RemoteException");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException");
            e.printStackTrace();
        }
    }

    public static void setServerAddress(){
        Address address = new Address();
        address.setIdentifier(1);
        address.setAddress("xx");
        address.setCity("yy");
        address.setProvince("zz");
        address.setCountry("CN");
        address.setPostalCode("10086");
        address.setExist(false);
        address.setArray(new String[]{"1","2","3"});

        try {
            String url =
                    "http://localhost:8080/test/services/AddressManager?wsdl";
            //生成服务对象Service
            Service service = new Service();
            Call call = (Call) service.createCall();

            //生成QName,两个参数与服务端的发布时的XML设定有关
            QName qn = new QName("urn:AddressManager","Address");

            //注册参数
            call.registerTypeMapping(Address.class, qn,
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(Address.Python HTTP 使用请求请求二进制数据

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板