-
Notifications
You must be signed in to change notification settings - Fork 379
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
Comments
你好。你是说纯的http服务吗?还是http传输的RPC服务(需要写protobuf的那种)。 |
纯粹的http server是workflow里提供的,这里好像不能使用RPC server相关的操作。 |
你好,这个和http没有什么关系哈,是因为http里默认用了protobuf转json,然后这个本质是protobuf默认值的用法问题,用JsonPrintOptions可以解决,可以参考这个文档: 在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
} |
@Barenboim @holmes1412 非常感谢两位的回答, 我想了解的就是srpc如何设置protobuf的默认值, 回答很好的解决了我的疑惑, 非常感谢 |
srpc搭建http服务时, 回包如何通过JsonPrintOptions设置打印格式
The text was updated successfully, but these errors were encountered: