如何用protobuf代表对象的未命名列表?

时间:2019-04-02 12:20:10

标签: java spring protocol-buffers protobuf-java

我正在尝试使用Spring RestTemplate来使用REST api,这几乎是我想要做的:

    public ResponseEntity<OfficeProto.Offices> getAllOffices() {
        return restTemplate.exchange(uri, HttpMethod.GET, httpEntity, new ParameterizedTypeReference<OfficeProto.Offices>() {});
    }

以下是源json的样子,如何在protobuf中表示未命名列表?

[
  {
    "name": "Office 1"
  },
  {
    "name": "Office 2"
  }
]

这是我的.proto文件的样子

syntax = "proto3";

option java_outer_classname = "OfficeProto";

message Office {
 string name = 1;
}

message Offices {
    repeated Office office = 1;
}

我收到以下错误:

org.springframework.web.client.RestClientException: Error while extracting response for type [class com.findwise.connect.OfficeProto$Offices] and content type [application/json]; nested exception is com.googlecode.protobuf.format.JsonFormat$ParseException: 1:1: Expected "{".

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试使用protobuf使用通用 JSON API。但是,protobuf 不是通用JSON序列化程序-它具有的JSON支持非常有据可依,并且它所提供的观点不适合您的情况。< / p>

因此:在这种情况下,请勿尝试使用protobuf。使用更通用的JSON工具。

作为简化生活的一般指导,如果protobuf是 write 编写的,则仅应将protobuf用作解析器(并且您恰好需要文本API而不是protobuf首选的二进制API) )。这样,您知道这些意见至少会匹配。