Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

srpc搭建http服务时, 回包如何通过JsonPrintOptions设置打印格式 #346

Closed
archliu321 opened this issue Nov 16, 2023 · 4 comments

Comments

@archliu321
Copy link

srpc搭建http服务时, 回包如何通过JsonPrintOptions设置打印格式

@Barenboim
Copy link
Contributor

你好。你是说纯的http服务吗?还是http传输的RPC服务(需要写protobuf的那种)。

@Barenboim
Copy link
Contributor

纯粹的http server是workflow里提供的,这里好像不能使用RPC server相关的操作。

@holmes1412
Copy link
Contributor

你好,这个和http没有什么关系哈,是因为http里默认用了protobuf转json,然后这个本质是protobuf默认值的用法问题,用JsonPrintOptions可以解决,可以参考这个文档:
https://github.com/sogou/srpc/blob/master/docs/docs-07-srpc-http.md

在protobuf转json的过程中,SRPC在RPCContext上提供了几个接口,支持 JsonPrintOptions。

示例:

class ExampleServiceImpl : public Example::Service
{
public:                                                                         
    void Echo(EchoRequest *req, EchoResponse *resp, RPCContext *ctx) override
    {
        resp->set_message("Hi back");
        resp->set_error(0); // 0是error类型int32在proto3中的默认值
        ctx->set_json_always_print_primitive_fields(true); // 带上所有原始域
        ctx->set_json_add_whitespace(true); // 增加json格式的空格
    }
};

不设置的话,输出如下:

{"message":"Hi back"}

通过RPCContext设置过json options之后的输出:

{
 "message": "Hi back",
 "error": 0
}

@archliu321
Copy link
Author

@Barenboim @holmes1412 非常感谢两位的回答, 我想了解的就是srpc如何设置protobuf的默认值, 回答很好的解决了我的疑惑, 非常感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants