从现有 Java 对象或 xsd 生成 .proto 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4660203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:24:14  来源:igfitidea点击:

Generating .proto file from existing Java objects or xsd

javaxsdprotocol-buffers

提问by ConfusedProtocolBuffer

Does anyone know of any tools that will generate a .proto file? Either from an existing xsd or from existing Java objects?

有谁知道任何可以生成 .proto 文件的工具?来自现有的 xsd 还是来自现有的 Java 对象?

I am using Apache CXFto make a web service call. The returned object has been auto generated from an xsd.

我正在使用Apache CXF进行网络服务调用。返回的对象是从 xsd 自动生成的。

ok so I now want to pass this data using protocol buffers to another system. The object model is very complex and changes regularly - i.e. I get a new xsd every time the object model has changed. I would like to be able to auto generate a .proto file so that I can send data using google protocol buffers.

好的,所以我现在想使用协议缓冲区将此数据传递到另一个系统。对象模型非常复杂并且会定期更改 - 即每次对象模型更改时我都会得到一个新的 xsd。我希望能够自动生成一个 .proto 文件,以便我可以使用谷歌协议缓冲区发送数据。

I have been looking everywhere but have come stuck.

我到处找,但都卡住了。

I am happy to

我很高兴

i.) generate .proto file from the xsd or ii.) generate .proto file from the Java objects (which have been generated from the xsd)

i.) 从 xsd 生成 .proto 文件或 ii.) 从 Java 对象(已从 xsd 生成)生成 .proto 文件

I cannot find any way of doing this already short of writing something myself... Your help much appreciated

我找不到任何方法来做这件事,因为我自己写了一些东西......非常感谢你的帮助

回答by Aravind Yarram

I believe that proto-stuffwill be helpful to you to some extent. I am not sure it generates the .proto file for you but protostuff-runtimeautomatically enables your existing pojos to ser/deser to various formats.

我相信proto-stuff会在一定程度上对你有所帮助。我不确定它会为您生成 .proto 文件,但protostuff-runtime 会自动使您现有的pojo能够将 ser/deser 转换为各种格式。

回答by Joe Wood

The Tranchis utility converts .XSD files to both .Proto and Thrift.

Tranchis 实用程序将 .XSD 文件转换为 .Proto 和 Thrift。

https://github.com/tranchis/xsd2thrift

https://github.com/tranchis/xsd2thrift

It works quite well for converting basic schema types from XML Schema.

它非常适合从 XML 模式转换基本模式类型。

回答by Alex

Try protostuff-runtime-protoproject from Github. If you have protostuff RuntimeSchema, you can convert it to the .proto file by this code:

尝试protostuff-runtime-proto来自 Github 的项目。如果您有 protostuff RuntimeSchema,则可以通过以下代码将其转换为 .proto 文件:

System.out.println(Generators.newProtoGenerator(schema).generate());