創(chuàng)建WebService服務(wù):
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比鼓樓網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式鼓樓網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋鼓樓地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
Idea -> New Project -> Spring Initializer -> web頁選擇web service模塊 + lombok 模塊
創(chuàng)建WebServiceConfig,主要配置webservice相關(guān)配置:
@Configuration
public class WebServiceConfig {
@Autowired
private ValidateWaferIdService validateWaferIdService;
/**
* 注入servlet bean name不能dispatcherServlet 否則會覆蓋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)建對應(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);
}
對服務(wù)接口進行具體實現(xiàn),其中注意對應(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\"}";
}
}
啟動服務(wù),由于endpoint 其publish在 /webservice目錄下,訪問 http://localhost:8090/webservice 即可發(fā)現(xiàn)對應(yīng)的入口,以及對應(yīng)的WSDL文件對應(yīng)的鏈接
如果要部署到服務(wù)器上,例如阿里云,則要注意對應(yīng)啟動服務(wù)后要通過安全組打開對應(yīng)的端口,否則外部調(diào)用無法訪問。
C++ 創(chuàng)建客戶端:
下載google gsoap2.8版本
windows下,進入gsoap/bin/win32目錄,錄得對應(yīng)相對目錄路徑,然后cmd下cd入當前目錄路徑。
依次執(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
其中,第二個命令用來生成對應(yīng)的函數(shù)頭文件,第三個命令會對應(yīng)生成需要的關(guān)聯(lián)文件以及對應(yīng)的cpp文件,執(zhí)行完會看到在對應(yīng)目錄下生成很多文件。
新建一個空C++項目,把所有生成的文件copy進去,另外把gsoap目錄下的stdsoap2.h和stdsoap2.cpp文件也copy到項目里。
新建一個mian函數(shù),即可嘗試調(diào)用,例如本例中根據(jù)對應(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具體實現(xiàn)
分享網(wǎng)址:http://aaarwkj.com/article38/igscsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、移動網(wǎng)站建設(shè)、手機網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站導(dǎo)航、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)