springboot 整合 cxf 發(fā)布webservice與調(diào)用

一凫海、什么是webservice

Web Service技術(shù)洽胶, 能使得運(yùn)行在不同機(jī)器上的不同應(yīng)用無(wú)須借助附加的塔橡、專門的第三方軟件或硬件图焰, 就可相互交換數(shù)據(jù)或集成启盛。依據(jù)Web Service規(guī)范實(shí)施的應(yīng)用之間蹦掐, 無(wú)論它們所使用的語(yǔ)言技羔、 平臺(tái)或內(nèi)部協(xié)議是什么, 都可以相互交換數(shù)據(jù)卧抗。Web Service是自描述藤滥、 自包含的可用網(wǎng)絡(luò)模塊, 可以執(zhí)行具體的業(yè)務(wù)功能社裆。Web Service也很容易部署拙绊, 因?yàn)樗鼈兓谝恍┏R?guī)的產(chǎn)業(yè)標(biāo)準(zhǔn)以及已有的一些技術(shù),諸如標(biāo)準(zhǔn)通用標(biāo)記語(yǔ)言下的子集XML、HTTP标沪。Web Service減少了應(yīng)用接口的花費(fèi)榄攀。Web Service為整個(gè)企業(yè)甚至多個(gè)組織之間的業(yè)務(wù)流程的集成提供了一個(gè)通用機(jī)制。

二金句、webservice服務(wù)端

可以將注解寫到接口當(dāng)中檩赢,本例接口無(wú)注解
@WebService(serviceName = "weather", // 與接口中指定的name一致
targetNamespace = "http://cn.gc.service", // 與接口中的命名空間一致,一般是接口的包名倒寫
endpointInterface = "com.zhongbaowd.services.entrance.control.webservice.WeatherInterface"http:// 接口地址
)

package cn.itcast.ws.jaxws.ws;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
 
/**
 * 例:天氣查詢
 * 通過注解可自定義如方法名,參數(shù)名等信息
 */
@WebService(
        targetNamespace="http://cn.gc.service", //命名空間
        name="WeatherWSSoap",   
        portName="WeatherWSSoapPort",
        serviceName="WeatherWS"   //該webservice服務(wù)的名稱
        )//@WebService表示該類是一個(gè)服務(wù)類违寞,需要發(fā)布其中的public的方法
//@BindingType(SOAPBinding.SOAP12HTTP_BINDING)//標(biāo)識(shí)使用soap1.1 還是1.2協(xié)議通信,默認(rèn)1.1
public class WeatherInterfaceImpl implements WeatherInterface {
 
    @WebMethod(
            operationName="queryWeather", //發(fā)布的方法名稱
            exclude=false   // 默認(rèn)fase: 表示發(fā)布該方法  true:表示不發(fā)布此方法
            )
         //WebResult :返回值名稱 WebParam:參數(shù)名稱
    @Override
    public @WebResult(name="result")String queryWeather(@WebParam(name="cityName")String cityName) {
    
        return "天氣良好";
    }
 
}

三贞瞒、springboot cxf 配置webservice 發(fā)布

1.導(dǎo)入必要jar包

  <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.0</version>
        </dependency>

        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.0</version>
        </dependency>

2.配置發(fā)布
- 默認(rèn)服務(wù)發(fā)布在Host:port/services/*路徑下 ,如需改變默認(rèn)路徑可配置ServletRegistrationBean
- endpoint.publish("/weather"); 發(fā)布的路徑趁曼,此時(shí)訪問路徑為:localhost:20006/ws-api/weather?wsdl
- 多個(gè)發(fā)布需多個(gè)Endpoint @Bean

import com.zhongbaowd.services.entrance.control.webservice.EsbWebService;
import com.zhongbaowd.services.entrance.control.webservice.EsbWebServiceImpl;
import com.zhongbaowd.services.entrance.control.webservice.WeatherInterface;
import com.zhongbaowd.services.entrance.control.webservice.WeatherInterfaceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * Created by yanfazhongxin on 2018/10/16.
 */
@Configuration
public class EsbWebServiceConfig {
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/ws-api/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public EsbWebService esbWebService() {
        return new EsbWebServiceImpl();
    }

    @Bean
    public WeatherInterface weatherInterface() {
        return new WeatherInterfaceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), esbWebService());
        endpoint.publish("/apis");
        return endpoint;
    }

    @Bean
    public Endpoint endpoint2() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), weatherInterface());
        endpoint.publish("/weather");
        return endpoint;
    }

四军浆、通過瀏覽器訪問發(fā)布的webservice服務(wù)wsdl

訪問http://localhost:20006/ws-api/weather?wsdl
顯示出wsdl說明發(fā)布成功

image.png

五、生成客戶端代碼

可利用jdk bin目錄下有的wsimport.exe程序生成客戶端代碼
命令參數(shù)
-keep:是否生成java源文件
-d:指定.class文件的輸出目錄
-s:指定.java文件的輸出目錄
-p:定義生成類的包名挡闰,不定義的話有默認(rèn)包名
-verbose:在控制臺(tái)顯示輸出信息
-b:指定jaxws/jaxb綁定文件或額外的schemas
-extension:使用擴(kuò)展來支持SOAP1.2

-s 指向項(xiàng)目java目錄 -p 包名一定要寫乒融,否則默認(rèn)服務(wù)端的類包名,生成之后會(huì)報(bào)錯(cuò)
wsimport -keep -s D:\IDESpaceWork\zhongbaowd-services\ws-entrance-control-service\src\main\java -p com.zhongbaowd.services.entrance.control.wsimport.resource.weather http://localhost:20006/ws-api/weather?wsdl

image.png

優(yōu)點(diǎn):調(diào)用方便
弊端:生成的代碼中包含了服務(wù)端的wsdl地址摄悯,在服務(wù)端有ip更改時(shí)可直接更改簇抵。但是如果方法名等更改了需要重新生成代碼

六、客戶端調(diào)用

1.在wsdl的最下方就可以看出客戶端如何調(diào)用
2.如需設(shè)置超時(shí)時(shí)間等射众,需要用到cxf的ClientProxy 代理

image.png
package com.zhongbaowd.services.entrance.control.wsimport.client;


import com.zhongbaowd.services.entrance.control.wsimport.resource.weather.WeatherWS;
import com.zhongbaowd.services.entrance.control.wsimport.resource.weather.WeatherWSSoap;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
/**
 * weather webservice客戶端
 * Created by yanfazhongxin on 2018/10/19.
 */
public class WeatherClient {

    /**
     * 查詢天氣
     * @param cityName
     * @return
     */
    public static String queryWeather(String cityName){
        WeatherWS weatherWS = new WeatherWS();
        WeatherWSSoap weatherWSSoapPort = weatherWS.getWeatherWSSoapPort();
        Client proxy = ClientProxy.getClient(weatherWSSoapPort );
        HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(300 * 1000L);// 連接超時(shí)時(shí)間
        policy.setReceiveTimeout(300 * 1000L);// 請(qǐng)求超時(shí)時(shí)間
        conduit.setClient(policy);
        return weatherWSSoapPort.queryWeather(cityName);
    }

    public static void main(String[] args) {
        String s = queryWeather("成都");
        System.out.println(s);
    }
}
image.png

七碟摆、HttpClient作為客戶端調(diào)用webservice

首先引入jar包

  <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>

HttpClient作為客戶端調(diào)用
優(yōu)點(diǎn):工具類復(fù)用,調(diào)用方便叨橱,測(cè)試接口時(shí)方便典蜕,可快速更換接口地址,而不需要重新生成代碼罗洗,而且可直接控制請(qǐng)求超時(shí)時(shí)間等 (推薦這種方式)

package com.zhongbaowd.services.entrance.control.httpClient;

import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class HttpClientSoapUtil{

    static int socketTimeout = 30000;// 請(qǐng)求超時(shí)時(shí)間
    static int connectTimeout = 30000;// 傳輸超時(shí)時(shí)間
    static Logger logger = Logger.getLogger(HttpClientSoapUtil.class);

    /**
     * 使用SOAP1.1發(fā)送消息愉舔,可調(diào)1.1,也可調(diào)用1.2
     *
     * @param postUrl
     * @param soapXml
     * @param soapAction
     * @return
     */
    public static String doPostSoap1_1(String postUrl, String soapXml,
                                       String soapAction) {
        String retStr = "";
        // 創(chuàng)建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(postUrl);
        //  設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(socketTimeout)
                .setConnectTimeout(connectTimeout).build();
        httpPost.setConfig(requestConfig);
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", soapAction);
            StringEntity data = new StringEntity(soapXml,
                    Charset.forName("UTF-8"));
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印響應(yīng)內(nèi)容
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
                logger.info("response:" + retStr);
            }
            // 釋放資源
            closeableHttpClient.close();
        } catch (Exception e) {
            logger.error("exception in doPostSoap1_1", e);
        }
        return retStr;
    }

    /**
     * 使用SOAP1.2發(fā)送消息,只能調(diào)用1.2
     *
     * @param postUrl
     * @param soapXml
     * @param soapAction
     * @return
     */
    public static String doPostSoap1_2(String postUrl, String soapXml,
                                       String soapAction) {
        String retStr = "";
        // 創(chuàng)建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(postUrl);
        // 設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(socketTimeout)
                .setConnectTimeout(connectTimeout).build();
        httpPost.setConfig(requestConfig);
        try {
            httpPost.setHeader("Content-Type",
                    "application/soap+xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", soapAction);
            StringEntity data = new StringEntity(soapXml,
                    Charset.forName("UTF-8"));
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印響應(yīng)內(nèi)容
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
                logger.info("response:" + retStr);
            }
            // 釋放資源
            closeableHttpClient.close();
        } catch (Exception e) {
            logger.error("exception in doPostSoap1_2", e);
        }
        return retStr;
    }

    public static void main(String[] args) {
        String orderSoapXml = "<?xml version = \"1.0\" ?>"
                + "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservices.b.com\">"
                + "   <soapenv:Header/>"
                + "   <soapenv:Body>"
                + "      <web:order soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
                + "         <in0 xsi:type=\"web:OrderRequest\">"
                + "            <mobile xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">?</mobile>"
                + "            <orderStatus xsi:type=\"xsd:int\">?</orderStatus>"
                + "            <productCode xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">?</productCode>"
                + "         </in0>" + "      </web:order>"
                + "   </soapenv:Body>" + "</soapenv:Envelope>";
        String querySoapXml = "<?xml version = \"1.0\" ?>"
                + "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservices.b.com\">"
                + "   <soapenv:Header/>"
                + "   <soapenv:Body>"
                + "      <web:query soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
                + "         <in0 xsi:type=\"web:QueryRequest\">"
                + "            <endTime xsi:type=\"xsd:dateTime\">?</endTime>"
                + "            <mobile xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">?</mobile>"
                + "            <startTime xsi:type=\"xsd:dateTime\">?</startTime>"
                + "         </in0>" + "      </web:query>"
                + "   </soapenv:Body>" + "</soapenv:Envelope>";
        String postUrl = "http://localhost:8080/services/WebServiceFromB";
        //采用SOAP1.1調(diào)用服務(wù)端伙菜,這種方式能調(diào)用服務(wù)端為soap1.1和soap1.2的服務(wù)
        doPostSoap1_1(postUrl, orderSoapXml, "");
        doPostSoap1_1(postUrl, querySoapXml, "");

        //采用SOAP1.2調(diào)用服務(wù)端轩缤,這種方式只能調(diào)用服務(wù)端為soap1.2的服務(wù)
        //doPostSoap1_2(postUrl, orderSoapXml, "order");
        //doPostSoap1_2(postUrl, querySoapXml, "query");
    }
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市贩绕,隨后出現(xiàn)的幾起案子火的,更是在濱河造成了極大的恐慌,老刑警劉巖淑倾,帶你破解...
    沈念sama閱讀 210,914評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件馏鹤,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡娇哆,警方通過查閱死者的電腦和手機(jī)湃累,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,935評(píng)論 2 383
  • 文/潘曉璐 我一進(jìn)店門勃救,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人治力,你說我怎么就攤上這事蒙秒。” “怎么了宵统?”我有些...
    開封第一講書人閱讀 156,531評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵税肪,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我榜田,道長(zhǎng)益兄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,309評(píng)論 1 282
  • 正文 為了忘掉前任箭券,我火速辦了婚禮净捅,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘辩块。我一直安慰自己蛔六,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,381評(píng)論 5 384
  • 文/花漫 我一把揭開白布废亭。 她就那樣靜靜地躺著国章,像睡著了一般。 火紅的嫁衣襯著肌膚如雪豆村。 梳的紋絲不亂的頭發(fā)上液兽,一...
    開封第一講書人閱讀 49,730評(píng)論 1 289
  • 那天,我揣著相機(jī)與錄音掌动,去河邊找鬼四啰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛粗恢,可吹牛的內(nèi)容都是我干的柑晒。 我是一名探鬼主播,決...
    沈念sama閱讀 38,882評(píng)論 3 404
  • 文/蒼蘭香墨 我猛地睜開眼眷射,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼匙赞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起妖碉,我...
    開封第一講書人閱讀 37,643評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤涌庭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后嗅绸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脾猛,經(jīng)...
    沈念sama閱讀 44,095評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡撕彤,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,448評(píng)論 2 325
  • 正文 我和宋清朗相戀三年鱼鸠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了猛拴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,566評(píng)論 1 339
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蚀狰,死狀恐怖愉昆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情麻蹋,我是刑警寧澤跛溉,帶...
    沈念sama閱讀 34,253評(píng)論 4 328
  • 正文 年R本政府宣布,位于F島的核電站扮授,受9級(jí)特大地震影響芳室,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜刹勃,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,829評(píng)論 3 312
  • 文/蒙蒙 一堪侯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧荔仁,春花似錦伍宦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,715評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至遇骑,卻和暖如春卖毁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背落萎。 一陣腳步聲響...
    開封第一講書人閱讀 31,945評(píng)論 1 264
  • 我被黑心中介騙來泰國(guó)打工势篡, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人模暗。 一個(gè)月前我還...
    沈念sama閱讀 46,248評(píng)論 2 360
  • 正文 我出身青樓禁悠,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親兑宇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子碍侦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,440評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容