美文网首页Java学习笔记
使用CXF调用Web Service服务

使用CXF调用Web Service服务

作者: liuyan731 | 来源:发表于2017-11-18 20:49 被阅读231次

工作也快半年了,时间很快,发现自己越来越懒了,书都堆灰了...平时工作虽忙但是周末不忙啊哈哈,看了zhoumorvan大神的博客和身边给力的小伙伴们,决定以后还是多看看多写写多分享。

记录是一个极好的习惯,我们会的知识中,有很大一部分其实是相对碎片的,很容易忘记。将它们整理并记下来是一个结构化知识的过程,也是一个整理和总结的过程。

工作第一篇,来个极简的:最近接入了人口信息应用平台的Web Service服务,第一次接触,就说一说使用CFX来调用Web Service服务的方式(没写如何发布Web Service服务,因为没做,可参考CXF和Spring整合发布基于Maven在Spring中集成CXF Web Service框架)。

CXF调用Web Service服务

1、下载CXF,解压即用(环境变量)

2、主要使用wadl2java这个工具,将Web Service发布的服务生成客户端java代码

 wsdl2java -encoding utf-8 -p com.test.nciic -d D:\\src -all  NciicServices.wsdl
  • encoding:编码
  • p:package(这个包的结构需要注意,需要与代码中的包结构一致)
  • d:代码生成目录

3、通过CXF的JaxWsProxyFactoryBean类来调用Web Service服务

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(NciicServicesPortType.class);
factory.setAddress("https://ws.nciic.org.cn/nciic_ws/services/NciicServices");
private NciicServicesPortType client = (NciicServicesPortType) factory.create();
String responseXmlStr = client.nciicCheck(nciicAuthKey, requestXmlStr); // 调用nciic服务

也可以集成在spring mvc中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
    <!-- 
        使用<jaxws:clietn>调用服务端
        jaxws:client内部使用JaxWsProxyFactoryBean方式
        serviceClass:指定portType地址(需要使用wsdl2java工具生成) 
    -->
    <jaxws:client id="client" address="https://ws.nciic.org.cn/nciic_ws/services/NciicServices"
        serviceClass="com.test.nciic.NciicServicesPortType">
    </jaxws:client>
</beans>
public void testCxfSpringClient(){
    //从Spring容器中取出portType
    NciicServicesPortType client = (NciicServicesPortType) applicationContext.getBean("client");
    String responseXmlStr = client.nciicCheck(nciicAuthKey, requestXmlStr); // 调用nciic服务
}

这里的NciicServicesPortType是CXF生成的Web Service接口类,里面定义了wsdl文件所描述的所有服务接口,形式如下:

/**
 * This class was generated by Apache CXF 3.1.13
 * 2017-10-27T17:18:40.243+08:00
 * Generated source version: 3.1.13
 * 
 */
@WebService(targetNamespace = "https://api.nciic.org.cn/NciicServices", name = "NciicServicesPortType")
@XmlSeeAlso({ObjectFactory.class})
public interface NciicServicesPortType {
@WebResult(name = "out", targetNamespace = "https://api.nciic.org.cn/NciicServices")
    @RequestWrapper(localName = "nciicCheck", targetNamespace = "https://api.nciic.org.cn/NciicServices", className = "com.test.nciic.NciicCheck")
    @WebMethod
    @ResponseWrapper(localName = "nciicCheckResponse", targetNamespace = "https://api.nciic.org.cn/NciicServices", className = "com.test.nciic.NciicCheckResponse")
    public String nciicCheck(
            @WebParam(name = "inLicense", targetNamespace = "https://api.nciic.org.cn/NciicServices")
                    String inLicense,
            @WebParam(name = "inConditions", targetNamespace = "https://api.nciic.org.cn/NciicServices")
                    String inConditions
    );
}

PS:Nciic这个是个自签名(或CNNIC证书)的https,还需要安装它网站的证书,参考StevenLian这个博客。附一个java添加证书信任列表的静态代码块:

static {
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
                new javax.net.ssl.HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
                        //域名或ip地址
                        if (hostname.equals("ws.nciic.org.cn")) {
                            return true;
                        }
                        return false;
                    }
                });
        System.setProperty("javax.net.ssl.trustStore", "C:\\jssecacerts");
        System.setProperty("javax.net.ssl.trustStorePassword", "changeit";
    }

PS:Nciic这个服务挺慢的(200ms),还挺贵的(按次收费)...我们在调用时加入memcache缓存,避免短时相同请求重复调用,并且将验证通过的结果写入到数据库中,先查缓存和数据库最后再用第三方服务。

思考

Web Service通过基于XML的SOAP来表示数据和请求,低耦合,在接口的发布和复用性上确实还行,接口的可读性也可以,但是确实还是没有调Http服务的api接口方便,很多国企和国家的对外服务貌似Web Service用的很多,但是在互联网公司貌似用的很少。Nciic服务使用Xml做请求和返回方式还是没有json方便,orz。


2017/11/18 done

此文章也同步至个人Github博客

相关文章

网友评论

    本文标题:使用CXF调用Web Service服务

    本文链接:https://www.haomeiwen.com/subject/jwjyvxtx.html