温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot创建WebService服务 + C++调用WebService具体实现

发布时间:2020-07-17 05:19:31 来源:网络 阅读:1364 作者:HelloGit 栏目:编程语言

创建WebService服务:


Idea -> New Project -> Spring Initializer -> web页选择web service模块 + lombok 模块


创建WebServiceConfig,主要配置webservice相关配置:


@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;

    }

}


创建对应的服务接口,定义API函数:


@WebService

public interface ValidateWaferIdService {

    @WebMethod

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

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

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

}


对服务接口进行具体实现,其中注意对应的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\"}";

        }

    }


启动服务,由于endpoint 其publish在 /webservice目录下,访问 http://localhost:8090/webservice  即可发现对应的入口,以及对应的WSDL文件对应的链接


如果要部署到服务器上,例如阿里云,则要注意对应启动服务后要通过安全组打开对应的端口,否则外部调用无法访问。


C++ 创建客户端:


下载google gsoap2.8版本


windows下,进入gsoap/bin/win32目录,录得对应相对目录路径,然后cmd下cd入当前目录路径。


依次执行以下命令:

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


其中,第二个命令用来生成对应的函数头文件,第三个命令会对应生成需要的关联文件以及对应的cpp文件,执行完会看到在对应目录下生成很多文件。


新建一个空C++项目,把所有生成的文件copy进去,另外把gsoap目录下的stdsoap2.h和stdsoap2.cpp文件也copy到项目里。


新建一个mian函数,即可尝试调用,例如本例中根据对应的函数入口,以下调用即可:


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;

}




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI