在 C/C++ 中实现 JSON RESTful 服务的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9788572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:17:18  来源:igfitidea点击:

Ways to implement a JSON RESTful service in C/C++

c++cjsonweb-services

提问by jllodra

I am trying to do a JSON Restful web service in C/C++. I have tried Axis2/C and Staff, which work great for XML serialization/deserialization but not for JSON.

我正在尝试用 C/C++ 做一个 JSON Restful Web 服务。我尝试过 Axis2/C 和 Staff,它们非常适合 XML 序列化/反序列化,但不适用于 JSON。

回答by Philipp

You might want to take a look at Casablancaintroduced in Herb Sutter's blog.

您可能想看看Herb Sutter 的博客中介绍的Casablanca

回答by Shaaban Ebrahim

there are a small number of libraries that support creating rest services with c, e.g. restinio:

有少数库支持使用 c 创建休息服务,例如restinio

#include <restinio/all.hpp>
int main()
{
    restinio::run(
        restinio::on_this_thread()
        .port(8080)
        .address("localhost")
        .request_handler([](auto req) {
            return req->create_response().set_body("Hello, World!").done();
        }));
    return 0;
}

回答by loentar

Try ngrest. It's a simple but fastC++ RESTful JSON Web Services framework. It can be deployed on top of Apache2, Nginx or own simple http server.

尝试ngrest。这是一个简单但快速的C++ RESTful JSON Web 服务框架。它可以部署在 Apache2、Nginx 或自己的简单 http 服务器之上。



Regarding Axis2/C with JSON. It's seems that official Axis2/C no longer maintained. So Axis2/C become obsolete (but still works).

关于带有 JSON 的 Axis2/C。似乎不再维护官方 Axis2/C。所以 Axis2/C 变得过时了(但仍然有效)。

JSON support for Axis2/C is available in axis2c-unofficialproject.

Axis2/C 的 JSON 支持在axis2c-非官方项目中可用。

There are an installation manuals on how to install Axis2/C with JSON support under Linux, Windows using binary package, Windows from source code.

有关于如何在LinuxWindows 使用二进制包Windows 源代码下安装支持 JSON 的 Axis2/C 的安装手册。

You can try it with WSF Staff using Customers (REST) example in JSON mode (which is available from staff/samples/rest/webclientdirectory of staff source code).

您可以在 JSON 模式下使用客户 (REST) 示例与 WSF Staff 一起尝试(可从staff/samples/rest/webclient员工源代码目录中获得)。

回答by Rutix

You could look at ffead-cpp. Apart from providing support for json and restfull web services it also includes more features. This framework may be too heavy weight for your situation though.

你可以看看ffead-cpp。除了提供对 json 和 restfull web 服务的支持之外,它还包括更多功能。不过,这个框架可能对您的情况来说太重了。

回答by Rami

try https://github.com/babelouest/ulfiusgreat library to build C/C++ Restful APIs. can support all platforms: Linux, FreeBSD, Windows and others

尝试 https://github.com/babelouest/ulfius很棒的库来构建 C/C++ Restful API。可以支持所有平台:Linux、FreeBSD、Windows 等

回答by georgeliatsos

For C++ web service, I am using the following stack:

对于 C++ Web 服务,我使用以下堆栈:

回答by Adam Gu

You may want to take a look at webcc.

您可能想看看webcc

It's a lightweight C++ REST and SOAP client and server library based on Boost.Asio (1.66+).

它是一个基于 Boost.Asio (1.66+) 的轻量级 C++ REST 和 SOAP 客户端和服务器库。

It's quite promising and actively being developed.

它很有前途并正在积极开发中。

There are great examples to demonstrate how to create a server and client.

有很好的示例来演示如何创建服务器和客户端。

Advantages:

好处:

  • The code is extremely clean (strictly following Google C++ Style).
  • No memory leak according to VLD test.
  • Easy-to-use APIs.
  • etc.
  • 代码非常干净(严格遵循 Google C++ 风格)。
  • 根据 VLD 测试没有内存泄漏。
  • 易于使用的 API。
  • 等等。

Disadvantages:

缺点:

  • No SSL support yet.
  • Cannot be deployed to existing web servers like Nginx because it manages its own HTTP server.
  • 尚不支持 SSL。
  • 无法部署到现有的 Web 服务器,如 Nginx,因为它管理自己的 HTTP 服务器。

回答by Marija Radezova

There is a JIRA projectresolved the support of JSON in AXIS2/C .
I implemented in my project and I managed with the writer (Badgerfish convention) but still I am trying to manage with the reader.
It seems more complicated managing with the stack in the memory.

有一个JIRA 项目解决了 AXIS2/C 对 JSON 的支持。
我在我的项目中实施并与作者一起管理(Badgerfish 约定),但我仍然试图与读者一起管理。
管理内存中的堆栈似乎更复杂。

回答by Dr. Alex RE

JSON and JSONPath are supported for both C and C++ in gsoapwith a new code generator and a new JSON APIto get you started quickly.

gsoap 中的 C 和 C++ 都支持 JSON 和 JSONPath,具有新的代码生成器和新的 JSON API,可让您快速入门。

Several JSON, JSON-RPC and REST examplesare included. Memory management is automatic.

包括几个 JSON、JSON-RPC 和 REST 示例。内存管理是自动的。

The code generator can be useful. Take for example the json.org menu.jsonsnippet:

代码生成器很有用。以json.orgmenu.json片段为例:

{ "menu": {
    "id": "file",
    "value": "File",
    "popup": {
      "menuitem": [
        {"value": "New", "onclick": "CreateNewDoc()"},
        {"value": "Open", "onclick": "OpenDoc()"},
        {"value": "Close", "onclick": "CloseDoc()"}
      ]
    }
  }
}

The gsoap command jsoncpp -M menu.jsongenerates this code to populate a JSON value:

gsoap 命令jsoncpp -M menu.json生成此代码以填充 JSON 值:

value x(ctx);
x["menu"]["id"] = "file";
x["menu"]["value"] = "File";
x["menu"]["popup"]["menuitem"][0]["value"] = "New";
x["menu"]["popup"]["menuitem"][0]["onclick"] = "CreateNewDoc()";
x["menu"]["popup"]["menuitem"][1]["value"] = "Open";
x["menu"]["popup"]["menuitem"][1]["onclick"] = "OpenDoc()";
x["menu"]["popup"]["menuitem"][2]["value"] = "Close";
x["menu"]["popup"]["menuitem"][2]["onclick"] = "CloseDoc()";

Also reading parsed JSON values and JSONPath code can be generated by this tool.

此工具还可以生成读取解析的 JSON 值和 JSONPath 代码。

EDIT

编辑

To clarify, the jsoncpp command-linecode generator shows the API code to read and write JSON data by using a .json file as a template, which I found is useful to save time to write the API code to populate and extract JSON data. JSONPath query code can also be generated with this tool.

为了澄清起见jsoncpp 命令行代码生成器显示了使用 .json 文件作为模板来读取和写入 JSON 数据的 API 代码,我发现这有助于节省编写 API 代码以填充和提取 JSON 数据的时间。也可以使用此工具生成 JSONPath 查询代码。