我正在尝试将一些 POJO 从 Java 映射到 Protobuf (proto3)。其中一些包含列表。虽然使用 POJO 映射列表(例如 List)没有问题,但我收到了 UnsupportedOperationException。
示例List<Product>(这可以正常工作):
ProductProtobuf.Builder map(Product product);
@Mapping(target = "productsList", source = "products")
ResponseProtobuf.Builder map(Response response);
Run Code Online (Sandbox Code Playgroud)
示例List<String>(这不起作用):
@Mapping(target = "usersList", source = "users")
ResponseProtobuf.Builder map(Response response);
Run Code Online (Sandbox Code Playgroud)
另外,我有一些用于构建器的映射器:
public ResponseProtobuf.Builder responseBuilder() {
return ResponseProtobuf.newBuilder();
}
public ProductProtobuf build(ProductProtobuf.Builder builder) {
return builder.build();
}
Run Code Online (Sandbox Code Playgroud) 我一直在试图产生从基本GRPC客户端和服务器接口.proto服务定义在这里从GRPC官方回购。该文件中定义的相关服务(来自上面的链接)如下:
service RouteGuide {
rpc GetFeature(Point) returns (Feature) {}
rpc ListFeatures(Rectangle) returns (stream Feature) {}
rpc RecordRoute(stream Point) returns (RouteSummary) {}
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
}
Run Code Online (Sandbox Code Playgroud)
我运行的命令是 protoc --java_out=${OUTPUT_DIR} path/to/proto/file
根据 grpc 站点(特别是此处),RouteGuideGrpc.java包含一个基类RouteGuideGrpc.RouteGuideImplBase的 a应该是从protoc上面的命令生成的 RouteGuide 服务中定义的所有方法,但该文件不会为我生成。
有没有人遇到过类似的问题?官方文档只是不正确吗?有没有人对我可以做些什么来生成那个缺失的类有什么建议?
我尝试使用 Gradle 在 java 中构建 gRPC 和 protobuf 应用程序
我按照以下说明操作:https : //github.com/grpc/grpc-java/blob/master/README.md
问题是没有生成一个文件: *ServiceGrpc.java 但是对应的*ServiceGrpc.class文件在gradle build生成的build目录下。
我尝试使用命令 protoc 手动运行编译器,但我遇到了完全相同的问题(我使用的是 Ubuntu 18.04)
这是我的原型文件
syntax = "proto3";
option java_multiple_files=true;
option java_generic_services= true;
//...//
message Track {
int64 id = 1; //... }
service TrackService {
rpc Create(Track) returns (Response); }
//...
Run Code Online (Sandbox Code Playgroud)
文件 Track.java、TrackOrBuilder.java、TrackOuterClass.java 都在那里。以及它们在构建目录中对应的 .class 文件。
使用标志“option java_generic_services= true”,会生成 TrackService.java,并再次生成 .class 文件。
但是不管怎样,TrackServiceGrpc.java文件并没有被创建,与其对应的.class文件相反,这很混乱。
这是我的 build.gradle :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
}
}
plugins {
id …Run Code Online (Sandbox Code Playgroud) 我正在尝试动态解析Java中的给定.proto文件,以解码Protobuf编码的二进制文件。
我有以下解析方法,其中“ proto”字符串包含.proto文件的内容:
public static Descriptors.FileDescriptor parseProto (String proto) throws InvalidProtocolBufferException, Descriptors.DescriptorValidationException {
DescriptorProtos.FileDescriptorProto descriptorProto = DescriptorProtos.FileDescriptorProto.parseFrom(proto.getBytes());
return Descriptors.FileDescriptor.buildFrom(descriptorProto, null);
}
Run Code Online (Sandbox Code Playgroud)
但是,执行时,先前的方法将引发消息“协议消息标签的电线类型无效”的异常。我使用了Google的示例.proto文件,因此我认为它是有效的:https : //github.com/google/protobuf/blob/master/examples/addressbook.proto
这是堆栈跟踪:
15:43:24.707 [pool-1-thread-1] ERROR com.github.whiver.nifi.processor.ProtobufDecoderProcessor - ProtobufDecoderProcessor[id=42c8ab94-2d8a-491b-bd99-b4451d127ae0] Protocol message tag had invalid wire type.
com.google.protobuf.InvalidProtocolBufferException$InvalidWireTypeException: Protocol message tag had invalid wire type.
at com.google.protobuf.InvalidProtocolBufferException.invalidWireType(InvalidProtocolBufferException.java:115)
at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:551)
at com.google.protobuf.GeneratedMessageV3.parseUnknownField(GeneratedMessageV3.java:293)
at com.google.protobuf.DescriptorProtos$FileDescriptorSet.<init>(DescriptorProtos.java:88)
at com.google.protobuf.DescriptorProtos$FileDescriptorSet.<init>(DescriptorProtos.java:53)
at com.google.protobuf.DescriptorProtos$FileDescriptorSet$1.parsePartialFrom(DescriptorProtos.java:773)
at com.google.protobuf.DescriptorProtos$FileDescriptorSet$1.parsePartialFrom(DescriptorProtos.java:768)
at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:163)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:197)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:209)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:214)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
at com.google.protobuf.DescriptorProtos$FileDescriptorSet.parseFrom(DescriptorProtos.java:260)
at com.github.whiver.nifi.parser.SchemaParser.parseProto(SchemaParser.java:9)
at com.github.whiver.nifi.processor.ProtobufDecoderProcessor.lambda$onTrigger$0(ProtobufDecoderProcessor.java:103)
at org.apache.nifi.util.MockProcessSession.write(MockProcessSession.java:895)
at …Run Code Online (Sandbox Code Playgroud) 我正在使用 protobuf,并从以下 proto 文件生成 JAVA 类。
syntax = "proto3";
enum Greeting {
NONE = 0;
MR = 1;
MRS = 2;
MISS = 3;
}
message Hello {
Greeting greeting = 1;
string name = 2;
}
message Bye {
string name = 1;
}
option java_multiple_files = true;
Run Code Online (Sandbox Code Playgroud)
现在我需要向生成的文件添加一些代码,我发现可以使用自定义插件(https://developers.google.com/protocol-buffers/docs/reference/java- generated#plugins)。我正在尝试用 Java 生成该插件,类似这样。
public class Test {
PluginProtos.CodeGeneratorResponse.getDefaultInstance();
/* Code to get generated files from java_out and use the insertion points */
codeGeneratorResponse.writeTo(System.out);
}
Run Code Online (Sandbox Code Playgroud)
然后我跑
protoc --java_out=./classes --plugin=protoc-gen-demo=my-plugin --demo_out=. …Run Code Online (Sandbox Code Playgroud) 我正在使用以下配置build.gradle
plugins {
id "com.google.protobuf" version "0.8.17"
id "java"
}
group "de.prerna.aws.tests"
version "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
ext {
protobufVersion = "3.18.1"
}
dependencies {
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:4.0.0-rc-2'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.39.0"
}
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
all().each { task ->
task.plugins { …Run Code Online (Sandbox Code Playgroud) 我们使用 protobuf v.3 通过 HTTP 将消息从 C# 客户端传输到 Java 服务器。
消息原型如下所示:
message CLIENT_MESSAGE {
string message = 1;
}
Run Code Online (Sandbox Code Playgroud)
客户端和服务器都对字符串使用 UTF-8 字符编码。
当我们使用像“abc”这样的短字符串值时一切都很好,但是当我们尝试传输包含 198 个字符的字符串时,我们捕获了一个异常:
com.google.protobuf.InvalidProtocolBufferException:
While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.
Run Code Online (Sandbox Code Playgroud)
我们甚至尝试比较包含 protobuf 数据的字节数组,但没有找到解决方案。对于“aaa”字符串字节数组以以下字节开头:
10 3 97 97 97
其中 10 是 protobuf 字段编号,3 是字符串长度,69 65 67 是“aaa”。
对于字符串
“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”
其中包含 …
我正在尝试使用 protobuf 模型编写类的单元测试并在 Android Studio 中运行它们。
例如,这是一个简单的转换器测试
class UpdateConfigConverterTest {
private val testable = UpdateConfigConverter()
@Test
fun `should convert from proto model`() {
val url = "https://test.com"
val availableVersion = 1
val requiredVersion = 0
val result = testable.convert(
ProtoUpdateConfig.newBuilder()
.setAvailable(availableVersion)
.setRequired(requiredVersion)
.setUrl("https://test.com")
.build()
)
assertEquals(availableVersion, result.updateAvailableVersion)
assertEquals(requiredVersion, result.updateRequiredVersion)
assertEquals(url, result.url)
}
}
Run Code Online (Sandbox Code Playgroud)
被测类:
internal typealias ProtoUpdateConfig = UpdateConfig
internal class UpdateConfigConverter {
fun convert(source: ProtoUpdateConfig): UpdateConfig =
UpdateConfig(
updateAvailableVersion = source.available,
updateRequiredVersion = source.required,
url = source.url
)
} …Run Code Online (Sandbox Code Playgroud) 给定一个原型文件:
\nsyntax = "proto3";\npackage hello;\n\nmessage TopGreeting {\n NestedGreeting greeting = 1;\n}\n\nmessage NestedGreeting {\n Greeting greeting = 1;\n}\n\nmessage Greeting {\n string message = 1;\n}\nRun Code Online (Sandbox Code Playgroud)\n和代码:
\npublic class Main {\n public static void main(String[] args) {\n System.out.printf("From top: %s%n", newGreeting("\xec\x98\xa4\xeb\x8a\x98\xec\x9d\x80 \xeb\xac\xb4\xec\x8a\xa8 \xec\x9a\x94\xec\x9d\xbc\xec\x9e\x85\xeb\x8b\x88\xea\xb9\x8c?"));\n System.out.printf("Directly: %s%n", "\xec\x98\xa4\xeb\x8a\x98\xec\x9d\x80 \xeb\xac\xb4\xec\x8a\xa8 \xec\x9a\x94\xec\x9d\xbc\xec\x9e\x85\xeb\x8b\x88\xea\xb9\x8c?");\n System.out.printf("ByteString: %s", newGreeting("\xec\x98\xa4\xeb\x8a\x98\xec\x9d\x80 \xeb\xac\xb4\xec\x8a\xa8 \xec\x9a\x94\xec\x9d\xbc\xec\x9e\x85\xeb\x8b\x88\xea\xb9\x8c?").toByteString().toStringUtf8());\n }\n\n private static Hello.TopGreeting newGreeting(String message) {\n Hello.Greeting greeting = Hello.Greeting.newBuilder()\n .setMessage(message)\n .build();\n Hello.NestedGreeting nestedGreeting = Hello.NestedGreeting.newBuilder()\n .setGreeting(greeting)\n .build();\n return Hello.TopGreeting.newBuilder()\n .setGreeting(nestedGreeting)\n .build();\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n输出
\n …我正在尝试按照此博客文章https://redbyte.eu/en/blog/calling-java-from-go-using-grpc/在此存储库中https://github.com/khpeek /pdf-解析器. 运行后./gradlew build,项目结构如下:
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 classes\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 extracted-include-protos\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 google\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 protobuf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 any.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 api.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 compiler\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 plugin.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 descriptor.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 duration.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 empty.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 field_mask.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 source_context.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 struct.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 timestamp.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 type.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wrappers.proto\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 extracted-protos\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 generated\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 …Run Code Online (Sandbox Code Playgroud)