WebService 调用 以及解析返回得xml串

通过Axis访问webservice服务

  1. 需要得得包信息如下:
<!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-saaj</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- 邮件start -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
        <!-- 邮件end -->
        <!--parse dom4j-->
        <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
  1. 通过axis调用webservice方法
public class RequestUtils {
    private static Logger logger = LoggerFactory.getLogger(RequestUtils.class);
    //字符集
    private final static String encodingStyle = "utf-8";
    //WSDL的地址
    //private final static String endpoint = "http://10.10.100.37/csp/hsb/DHC.Published.PUB0016.BS.PUB0016.CLS?WSDL=1";
    private final static String endpoint = "webservice地址";
    //命名空间,在WSDL中对应的标签是:参见说明第3条                                    
    private final static String targetNamespace = "命名空间";
    //具体方法的调用URI,在WSDL中对应的标签是:参见说明第4条
    private final static String soapActionURI = "soapAction得url地址";
    //具体调用的方法名,在WSDL中对应的标签是:参见说明第5条
    private final static String method = "方法名称";
    //调用接口的参数的名字
    private final static String[] paramNames = {"input1","input2"};

    //排队接口
    public static String reqOrder() throws ServiceException, MalformedURLException, RemoteException {

        Service service = new Service();
        Call call = (Call) service.createCall();
//            call.setTimeout(new Integer(20000));  //设置超时时间
        call.setSOAPActionURI(soapActionURI);
        call.setTargetEndpointAddress(new URL(endpoint));  //设置目标接口的地址
        call.setEncodingStyle(encodingStyle);//设置传入服务端的字符集格式如utf-8等
        call.setOperationName(new QName(targetNamespace, method));// 具体调用的方法名,可以由接口提供方告诉你,也可以自己从WSDL中找
        call.setUseSOAPAction(true);
        call.addParameter(new QName(targetNamespace, paramNames[0]),
                XMLType.XSD_STRING,
                javax.xml.rpc.ParameterMode.IN);// 接口的参数
        call.addParameter(new QName(targetNamespace, paramNames[1]),
                XMLType.XSD_STRING,
                javax.xml.rpc.ParameterMode.IN);// 接口的参数
        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型  ,如String
        call.setReturnClass(java.lang.String.class); //返回字符串数组类型
        // 给方法传递参数,并且调用方法 ,如果无参,则new Object[]{}
        String result = (String) call.invoke(new Object[]{"参数值1", "参数值2"});
        // 返回得结果
        logger.info("jie:" + result);
        return result;
    }
}

3、说明

1.上面的代码需要您进行部分修改
2.关键的参数都先提出来解释了,这些参数都可以在WSDL文档中找到,搜变量名即可
3.在WSDL中对应的标签是
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" ... targetNamespace="http://WebXml.com.cn/">
4.在WSDL中对应的标签是
<soap:operation soapAction="http://WebXml.com.cn/getCharFonts" style="document"/>
5.在WSDL中对应的标签是
<wsdl:operation name="getCharFonts">
6.如果调用方法没有参数,那么call.addParameter那一行就不用了
7.有些接口提供方提供的WDSL中没有soapActionURI,那么就不用设置
8.具体参数的名字和类型,一般接口提供者会告诉你,如果是调用第三方的接口,可以自己从文档中找,比如在本例中,可以在WSDL文档中找到这个标签:
<soap:address location="http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx"/>,
具体参数的地址就为:http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx
  1. 解析xml串
    通过dom4j解析xml串,十分方便,上面已经引入得dom4j得包,此示例较为简单
返回得xml格式如下
	<root>
         <ResultCode>0</ResultCode>
         <ResultDesc>成功</ResultDesc>
	</root>
对应得解析代码:
public static String preParseXml(String xml){
		String str = "";
        Document doc = null;
        try {
            //将字符串转化未xml
            doc = DocumentHelper.parseText(xml);
            // 获取根节点
            Element rootElt = doc.getRootElement();
            // 拿到根节点下 ResultCode 和 ResultDesc的值
            String resultCode = rootElt.elementTextTrim("ResultCode");
            String resultDesc = rootElt.elementTextTrim("ResultDesc");
            str = "resultCode:" + resultCode + ",resultDesc: " + resultDesc;
        }catch(Exception e){
            e.printStackTrace();
        }
        return str;
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个小虾米

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值