首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java : io.protostuff.compiler.parser.ParserException:无法加载proto

Java : io.protostuff.compiler.parser.ParserException:无法加载proto
EN

Stack Overflow用户
提问于 2022-02-03 15:36:09
回答 1查看 140关注 0票数 1

我正在使用guice解析器来解析proto文件。它无法解决进口问题。对于如何解决同一项目内的进口和进口问题,专家们有什么建议吗?

原文件

代码语言:javascript
复制
syntax = "proto3";
     package grpc;
     
     import "envoyproxy/protoc-gen-validate/validate/validate.proto";
     import "google/api/annotations.proto";
     import "google/protobuf/timestamp.proto";
     import "google/rpc/status.proto";
     import "google/protobuf/struct.proto";
     
     option go_package = "bitbucket.mycompany.com";
     option java_multiple_files = true;
     
     message SendRequest {
         string data = 1 ;
         bytes document = 2 ;
     }
     
     message SendResponse {
         string Status = 1;
         string Message = 2;
         repeated string DocumentList = 3;
     }

guice解析器pom.xml中的依赖项

代码语言:javascript
复制
<dependency>
    <groupId>io.protostuff</groupId>
    <artifactId>protostuff-parser</artifactId>
    <version>3.1.38</version>
</dependency>
<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>5.1.0</version>
</dependency>

我的主要应用程序代码

代码语言:javascript
复制
    Path currentDir = Paths.get("src\\main\\resources");
    System.out.println(currentDir.toAbsolutePath());

    final Injector injector = Guice.createInjector(new ParserModule());
    final Importer importer = injector.getInstance(Importer.class); 
    final ProtoContext protoContext = importer.importFile(new LocalFileReader(currentDir), "test.proto");
        
    final Proto proto = protoContext.getProto();
    final List<Message> messages = proto.getMessages();
    System.out.println(String.format("Messages: %s", messages));   

运行应用程序时,我会遇到以下异常

代码语言:javascript
复制
Exception in thread "main" io.protostuff.compiler.parser.ParserException: Can not load proto: envoyproxy/protoc-gen-validate/validate/validate.proto not found
    at io.protostuff.compiler.parser.FileDescriptorLoaderImpl.load(FileDescriptorLoaderImpl.java:35)
    at io.protostuff.compiler.parser.ImporterImpl.importFile(ImporterImpl.java:34)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-04 02:45:09

引言

让我们将io.protostuff:protostuff-parser库的io.protostuff:protostuff-parser作为当前版本。

解决方案

有必要使用FileReaderFactoryFileReader接口来获得包含目录的支持。

解决方案的灵感来自:原料药-编译器/ProtostuffCompiler.java,v3.1.38 .原料药/原料药-编译器

示例程序

让我们假设导入使用了/some/directory/imports目录。

代码语言:javascript
复制
/some/directory/imports$ tree -d -L 2
.
├── envoyproxy
│   └── protoc-gen-validate
└── google
    ├── api
    ├── protobuf
    └── rpc

程序源代码:

代码语言:javascript
复制
import com.google.inject.Guice;
import com.google.inject.Injector;
import io.protostuff.compiler.ParserModule;
import io.protostuff.compiler.model.Message;
import io.protostuff.compiler.model.Proto;
import io.protostuff.compiler.parser.FileReader;
import io.protostuff.compiler.parser.FileReaderFactory;
import io.protostuff.compiler.parser.Importer;
import io.protostuff.compiler.parser.ProtoContext;
import java.nio.file.Path;
import java.util.List;

public final class Program {
    public static void main(final String[] args) {
        final Injector injector = Guice.createInjector(new ParserModule());

        final FileReaderFactory fileReaderFactory = injector.getInstance(FileReaderFactory.class);
        final List<Path> includePaths = List.of(
            Path.of("/some/directory/imports")
        );
        final FileReader fileReader = fileReaderFactory.create(includePaths);

        final Importer importer = injector.getInstance(Importer.class);
        final ProtoContext protoContext = importer.importFile(
            fileReader,
            "/some/other/directory/test.proto"
        );

        final Proto proto = protoContext.getProto();

        final List<Message> messages = proto.getMessages();
        System.out.println(String.format("Messages: %s", messages));
    }
}

程序输出:

代码语言:javascript
复制
Messages: [Message{name=SendRequest, fullyQualifiedName=.grpc.SendRequest, fields=[Field{name=data, typeName=string, tag=1, options=DynamicMessage{fields={}}}, Field{name=document, typeName=bytes, tag=2, options=DynamicMessage{fields={}}}], options=DynamicMessage{fields={}}}, Message{name=SendResponse, fullyQualifiedName=.grpc.SendResponse, fields=[Field{name=Status, typeName=string, tag=1, options=DynamicMessage{fields={}}}, Field{name=Message, typeName=string, tag=2, options=DynamicMessage{fields={}}}, Field{name=DocumentList, modifier=repeated, typeName=string, tag=3, options=DynamicMessage{fields={}}}], options=DynamicMessage{fields={}}}]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70974099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档