c++如何调用springboot服务
时间: 2023-08-11 17:30:36 浏览: 43
要在 C++ 中调用 Spring Boot 服务,可以使用 HTTP 客户端库来发送 HTTP 请求并接收响应。常用的 HTTP 客户端库有 libcurl、Boost.Beast 和 Poco。
以下是使用 libcurl 调用 Spring Boot 服务的示例代码:
```c++
#include <curl/curl.h>
#include <iostream>
int main() {
CURL *curl;
CURLcode res;
std::string url = "http://localhost:8080/api/hello-world";
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
curl_easy_cleanup(curl);
}
return 0;
}
```
此代码使用 libcurl 库发送 GET 请求到 Spring Boot 服务的 `/api/hello-world` 接口,并输出响应结果。
注意,在使用 libcurl 库之前,需要先安装 libcurl 库和其依赖。具体安装方法可以参考 libcurl 的官方文档。