【问题标题】:SpringBoot REST Json Request to ProtobufSpringBoot REST Json 请求到 Protobuf
【发布时间】:2021-06-12 10:25:36
【问题描述】:

我正在尝试将 json REST 请求转换为 protobuf 格式。

示例 JSON 输入:

{
    "id": 111,
    "name": "Anirban"
}

REST 控制器:

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody Student handleStudent(@Valid @RequestBody Student student){
    System.out.println("Name:" + student.getName());
    System.out.println("ID:" + event.getId());
    // A service to convert student to protobuf and send to other service
    return student;
}

原型文件:

syntax = "proto3";
package student;
option java_multiple_files = true;
option java_package = "com.student.protobuf";
option java_outer_classname = "StudentOuter";

message Student {
    int32 id = 1;
    string name = 2;
}

这里我想将收到的 Student json 请求转换为 protobuf 对象并发送到另一个服务。找不到任何合适的指南。有什么帮助吗?

【问题讨论】:

  • 您是否正在尝试生成该 proto 文件?如果是这样,您可以使用模板引擎,如 freemarker 或速度。对于这个非常小的用例可能有点矫枉过正,但如果 proto 文件变得更复杂,那肯定是有意义的。
  • protobuf 对象看起来很简单,所以你可以通过字符串连接来实现这个小用例。否则,您需要一个带有自己的序列化程序的 protobuf 对象。

标签: java spring-boot rest protocol-buffers


【解决方案1】:

您使用https://gitlab.com/protobuf-tools/proto_domain_converter 在从 REST 接收到的 Student 类与 protobuf 生成的 Student 类之间进行转换

【讨论】:

    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 2022-01-12
    • 2023-04-09
    • 2021-07-15
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2019-03-31
    相关资源
    最近更新 更多