webService的发布与调用

b4eef5c160103c622ee0a191d26a2607192.jpg

8a6081bd0067d823ad217257a12baa095be.jpg

80363aab5d4db03e41598fb0a7463da089b.jpg

feea81e10b717e183defde2d603f873eedb.jpg

新建webService方法类(可使用接口)

package com.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/**
 * @author chenhao
 * @date ${date}
 * <p>
 * 创建三个webService方法,分别是没有参数,有一个参数,有两个参数
 * 将该类三个方法进行发布
 * 静态方法和用final修饰的方法不会被发布
 * @WebService 注解后声明该类是webService, targetNamespace属性设置命名空间
 * 可以注解类,也可以注解接口
 */
@WebService(targetNamespace = "http://www.my.ws.test.com")
public class PushWebServiceTest {

    /**
     * @WebMethod 对方法的注解, 可不加
     * operationName属性是发布的方法名,不写或不使用注解,则使用默认方法名
     * @WebResult 对方法返回值的注解, 可不加
     * name 属性是发布方法返回值的名称,不写或不使用注解,则默认使用return为返回名称
     * @WebParam 对方法参数的注解, 可不加
     * name 属性是发布方法参数的名称,不写或不使用注解,则默认使用arg[index]为参数名
     */
    @WebMethod(operationName = "oneParam")
    public @WebResult(name = "result")
    String test1(@WebParam(name = "name") String name) {
        return "one param --> name is " + name;
    }

    @WebMethod(operationName = "noParam")
    public @WebResult(name = "result")
    String test() {
        return "no param";
    }

    @WebMethod(operationName = "towParam")
    public @WebResult(name = "result")
    String test2(@WebParam(name = "name") String name, @WebParam(name = "age") int age) {
        return "two param --> name is : " + name + " and age :" + age;
    }

    public static String test3() {
        return "静态方法不会被发布";
    }

    public final String test4() {
        return "用final修饰的方法不会被发布";
    }

}

新建发布webService的启动类

package com.service;

import javax.xml.ws.Endpoint;

/**
 * @author chenhao
 * @date ${date}
 * 发布webService服务
 */
public class Push {

    public static void main(String[] args) {
        Endpoint.publish("http://127.0.0.1:9001/service/ws", new PushWebServiceTest());
        System.out.println("发布成功!");
    }

}

启动成功后,webService服务就完成发布了.

然后开始调用webService,同样,新建一个web项目.引入依赖包

axis-1.4.jar

axis-jaxrpc-1.4.jar

commons-discovery-0.5.jar

commons-logging-1.2.jar

wsdl4j-1.6.3.jar

创建一个WebService调用的工具类

package com.web.utils;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Map;

/**
 * @author chenhao
 * @date ${date}
 */
public class WebServiceUtil {

    private static Service service;
    private static Call call;

    public static String excute(String url, String nameSpaceUrl, String methodName, QName returnType, Map<String, ?> params) throws ServiceException {
        return excute(url, nameSpaceUrl, methodName, returnType, params, String.class);
    }


    public static <T> T excute(String url, String nameSpaceUrl, String methodName, QName returnType, Map<String, ?> params, Class<T> clazz) throws ServiceException {
        URL endpointUrl = null;
        try {
            endpointUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new ServiceException("URL 初始化失败");
        }
        init();
        call.setTargetEndpointAddress(endpointUrl);
        call.setOperationName(new QName(nameSpaceUrl, methodName));
        Object[] objs = new Object[params.size()];
        if (params != null && params.size() > 0) {
            int i = 0;
            for (String key : params.keySet()) {
                call.addParameter(key, getXMLType(params.get(key).getClass()), ParameterMode.IN);
                objs[i] = params.get(key);
                i++;
            }
        }
        if (returnType != null) {
            call.setReturnType(returnType);
        }
        try {
            return (T) call.invoke(objs);
        } catch (RemoteException e) {
            String error = e.getMessage();
            if (error.indexOf("Unexpected wrapper element") != -1 && error.indexOf("Expected") != -1) {
                int count = error.split("}").length - 1;
                if (count == 2) {
                    error = error.substring(error.indexOf("}") + 1, error.length() - 1);
                }
                error = error.substring(error.indexOf("{") + 1, error.indexOf("}"));
                return excute(url, error, methodName, returnType, params, clazz);
            }
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public static QName getXMLType(Class clazz) {
        if (clazz != null) {
            if (clazz.getName().equals(String.class.getName())) {
                return XMLType.XSD_STRING;
            } else if (clazz.getName().equals(int.class.getName())) {
                return XMLType.XSD_INT;
            } else if (clazz.getName().equals(Integer.class.getName())) {
                return XMLType.XSD_INTEGER;
            } else if (clazz.getName().equals(boolean.class.getName()) || clazz.getName().equals(Boolean.class.getName())) {
                return XMLType.XSD_BOOLEAN;
            } else if (clazz.getName().equals(double.class.getName()) || clazz.getName().equals(Double.class.getName())) {
                return XMLType.XSD_DOUBLE;
            }
        }
        return XMLType.XSD_ANY;
    }

    private static void init() throws ServiceException {
        if (service == null) {
            service = new Service();
        }
        try {
            call = (Call) service.createCall();
        } catch (javax.xml.rpc.ServiceException e) {
            e.printStackTrace();
            throw new ServiceException("WebService 初始化失败");
        }
    }

}

通过上面的工具类,我们调用所需要的参数有调用的URL,命名空间,方法名,返回类型,参数

其中,URL就是service项目发布的链接http://127.0.0.1:9001/service/ws,在链接后面加上?wsdl,访问webService的XML文件

http://127.0.0.1:9001/service/ws?wsdl

96741c23a07433025c32aa71ebe65b7556d.jpg

如上图所示,获取命名空间,然后通过方法链接,访问方法的XML文件

d456165975946db6720ef491737e5437d52.jpg

如上图所示,我们得到了方法名,方法参数,返回名称和返回类型,调用工具类访问webService方法所需要的参数都已经有了

新建一个调用类

package com.web.client;

import com.web.utils.WebServiceUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chenhao
 * @date ${date}
 */
public class Web {

    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

    public static void test1() {
        Map<String, Object> params = new HashMap<>();
        try {
            String test = WebServiceUtil.excute("http://127.0.0.1:9001/service/ws", "http://www.my.ws.test.com", "noParam", org.apache.axis.encoding.XMLType.XSD_STRING, params, String.class);
            System.out.println(test);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void test2() {
        Map<String, Object> params = new HashMap<>();
        params.put("name", "张三");
        try {
            String test = WebServiceUtil.excute("http://127.0.0.1:9001/service/ws", "http://www.my.ws.test.com", "oneParam", org.apache.axis.encoding.XMLType.XSD_STRING, params, String.class);
            System.out.println(test);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void test3() {
        Map<String, Object> params = new HashMap<>();
        params.put("name", "张三");
        params.put("age", 20);
        try {
            String test = WebServiceUtil.excute("http://127.0.0.1:9001/service/ws", "http://www.my.ws.test.com", "towParam", org.apache.axis.encoding.XMLType.XSD_STRING, params, String.class);
            System.out.println(test);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

196b0ef32108b16496dbbb6f857da36d887.jpg

转载于:https://my.oschina.net/u/3719679/blog/2251420

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值