【问题标题】:Autogenerate REST endpoints from .proto从 .proto 自动生成 REST 端点
【发布时间】:2018-03-03 16:28:27
【问题描述】:

我在编译 .proto 文件时遇到问题。希望从 .proto 文件生成 REST 端点。以下是代码和错误: 语法 = "proto3";

package pb;

import "google/protobuf/empty.proto";
import "google/api/annotations.proto";

service UrlShortener {
  rpc Hello(HelloRequest) returns (HelloResponse);

  rpc Encrypt(EncryptRequest) returns (EncryptResponse);

  rpc Decrypt(DecryptRequest) returns (DecryptResponse) {
    option (google.api.http) = {
                get: "/{hash}"
            };
  }
}

message HelloRequest {
  string Name = 1;
}

message HelloResponse {
  string Message = 1;
}

message EncryptRequest {
  string OriginalUrl = 1;
}

message EncryptResponse {
  UrlMap ResponseMap = 1;
}

message DecryptRequest {
  string hash = 1;
}

错误: github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis:警告:目录不存在。 google/api/annotations.proto:找不到文件。 urlshortener.proto:导入“google/api/annotations.proto”未找到或有错误。

请帮助解决这个问题。

我试过了:去 -u github.com/grpc-ecosystem/grpc-gateway 但它没有说:路径中没有可构建的源文件。

【问题讨论】:

    标签: protocol-buffers grpc


    【解决方案1】:

    我认为你的定义中有不止一个错误

    您在定义的开头缺少语法版本:

    syntax = "proto3";
    

    有一些未定义的类型

    service.proto:32:3: "UrlMap" is not defined.
    service.proto:12:40: "DecryptResponse" is not defined.
    

    您正在导入且未使用的 empty.proto

    您可以使用来自

    的 googleapies
    {GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
    

    然后使用:

    protoc -I${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis -I/usr/local/include -I. service.proto --go_out=plugins=grpc:.
    

    我做了之前的修改,它编译了,所以我有了 service.pb.go 文件

    已编辑:

    看看这个 grpc-gateway,也许可以帮到你 https://github.com/grpc-ecosystem/grpc-gateway

    【讨论】:

    • 语法 = "proto3";存在于我的文件中,只是留在上面的代码块中,对于 UrlMap 结构也是如此。为粘贴未经编辑的版本道歉。我发现问题是 google/api/annotations 已经从之前的路径 grpc-ecosystem/grpc-gateway/third_party/googleapis 移动了。执行以下操作解决了错误:go get -u github.com/grpc-ecosystem/grpc-gateway/...
    【解决方案2】:

    找到解决方案:问题是 google/api/annotations 已从早期路径 grpc-ecosystem/grpc-gateway/third_party/googleapis 移动到 https://github.com/grpc-ecosystem/grpc-gateway/tree/master/third_party/googleapis/google/api

    运行以下解决了错误:go get -u github.com/grpc-ecosystem/grpc-gateway/...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2016-12-08
      • 2018-08-01
      • 1970-01-01
      相关资源
      最近更新 更多