SpringBoot創(chuàng)建WebService服務(wù)+C++調(diào)用WebService具體實(shí)現(xiàn)

創(chuàng)建WebService服務(wù):

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比鼓樓網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式鼓樓網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋鼓樓地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。

Idea -> New Project -> Spring Initializer -> web頁(yè)選擇web service模塊 + lombok 模塊

創(chuàng)建WebServiceConfig,主要配置webservice相關(guān)配置:

@Configuration

public class WebServiceConfig {

    @Autowired

    private ValidateWaferIdService validateWaferIdService;

    /**

     * 注入servlet  bean name不能dispatcherServlet 否則會(huì)覆蓋dispatcherServlet

     */

    @Bean(name = "cxfServlet")

    public ServletRegistrationBean<CXFServlet> cxfServlet() {

        return new ServletRegistrationBean<>(new CXFServlet(), "/webservice/*");

    }

    @Bean(name = Bus.DEFAULT_BUS_ID)

    public SpringBus springBus() {

        return new SpringBus();

    }

    @Bean(name = "WebServiceDemoEndpoint")

    public Endpoint sweptPayEndpoint1() {

        EndpointImpl endpoint = new EndpointImpl(springBus(), validateWaferIdService);

        endpoint.publish("/webservice");

        return endpoint;

    }

}

創(chuàng)建對(duì)應(yīng)的服務(wù)接口,定義API函數(shù):

@WebService

public interface ValidateWaferIdService {

    @WebMethod

    String DPMatchLot(@WebParam(name = "waferId") String waferId,

                      @WebParam(name = "startEnd") String startEnd,

                      @WebParam(name = "clientId") String clientId);

}

對(duì)服務(wù)接口進(jìn)行具體實(shí)現(xiàn),其中注意對(duì)應(yīng)的annotation要配置正確:

@Service

@WebService(serviceName = "DPMatchLot", // 與接口中指定的name一致

        targetNamespace = "http://webservice.frank.com", // 與接口中的命名空間一致,一般是接口的包名倒

        endpointInterface = "com.example.webservice.ValidateWaferIdService" // 接口地址

)

public class ValidateWaferIdServiceImpl implements ValidateWaferIdService {

    @Override

    public String DPMatchLot(String waferId, String startEnd, String clientId) {

        if (waferId.equals("CE0011737-19G5") && startEnd.equals("START") && clientId.equals("DWS001")) {

            return "{\"waferid\":\"EP0251785-18G1\",\"result\":\"ok\"}";

        } else if (waferId.equals("CE0011737-19G5") && startEnd.equals("START") && clientId.equals("DWS002")) {

            return "{\"waferid\":\"CE0011737-19G5\",\"ErrorNo\":\"0007\",\"message\":\"Wafer count not match.\",\"result\":\"Fail\"}";

        } else {

            return "{\"waferid\":\"CE0011737-19G5\",\"ErrorNo\":\"0008\",\"message\":\"The input wafer id format is incorrect.\",\"result\":\"Fail\"}";

        }

    }

啟動(dòng)服務(wù),由于endpoint 其publish在 /webservice目錄下,訪問(wèn) http://localhost:8090/webservice  即可發(fā)現(xiàn)對(duì)應(yīng)的入口,以及對(duì)應(yīng)的WSDL文件對(duì)應(yīng)的鏈接

如果要部署到服務(wù)器上,例如阿里云,則要注意對(duì)應(yīng)啟動(dòng)服務(wù)后要通過(guò)安全組打開對(duì)應(yīng)的端口,否則外部調(diào)用無(wú)法訪問(wèn)。

C++ 創(chuàng)建客戶端:

下載google gsoap2.8版本

windows下,進(jìn)入gsoap/bin/win32目錄,錄得對(duì)應(yīng)相對(duì)目錄路徑,然后cmd下cd入當(dāng)前目錄路徑。

依次執(zhí)行以下命令:

cd C:\Program Files (x86)\gsoap-2.8\gsoap\bin\win32

wsdl2h -o validate.h http://localhost:8090/webservice/webservice?wsdl

soapcpp2 -j validate.h

其中,第二個(gè)命令用來(lái)生成對(duì)應(yīng)的函數(shù)頭文件,第三個(gè)命令會(huì)對(duì)應(yīng)生成需要的關(guān)聯(lián)文件以及對(duì)應(yīng)的cpp文件,執(zhí)行完會(huì)看到在對(duì)應(yīng)目錄下生成很多文件。

新建一個(gè)空C++項(xiàng)目,把所有生成的文件copy進(jìn)去,另外把gsoap目錄下的stdsoap2.h和stdsoap2.cpp文件也copy到項(xiàng)目里。

新建一個(gè)mian函數(shù),即可嘗試調(diào)用,例如本例中根據(jù)對(duì)應(yīng)的函數(shù)入口,以下調(diào)用即可:

int main(int argc, const char* argv[])

{

DPMatchLotSoapBindingProxy proxy;

ns2__DPMatchLot waferElement;

ns2__DPMatchLotResponse errCode;

//Case #1 ---- A sample of Successful call of Web Service

string waferId = "CE0011737-19G5";

waferElement.waferId = &waferId;

string isHeader = "START";

waferElement.startEnd = &isHeader;

string clientId = "DWS001";

waferElement.clientId = &clientId;

if (proxy.DPMatchLot(&waferElement, errCode) == SOAP_OK)

{

cout << "SOAP CALL succeeded!" << endl;

cout << *errCode.return_ << endl;

}

else

{

cout << "SOAP CALL failed!" << endl;

cout << *errCode.return_ << endl;

}

網(wǎng)站題目:SpringBoot創(chuàng)建WebService服務(wù)+C++調(diào)用WebService具體實(shí)現(xiàn)
分享網(wǎng)址:http://bm7419.com/article38/igscsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司移動(dòng)網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站導(dǎo)航、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)