此内容没有您所选择的语言版本。
Chapter 247. Protobuf DataFormat
Available as of Camel version 2.2.0
247.1. Protobuf - Protocol Buffers 复制链接链接已复制到粘贴板!
"Protocol Buffers - Google’s data interchange format"
Camel provides a Data Format to serialize between Java and the Protocol Buffer protocol. The project’s site details why you may wish to choose this format over xml. Protocol Buffer is language-neutral and platform-neutral, so messages produced by your Camel routes may be consumed by other language implementations.
247.2. Protobuf Options 复制链接链接已复制到粘贴板!
The Protobuf dataformat supports 3 options which are listed below.
Name | Default | Java Type | Description |
---|---|---|---|
instanceClass |
| Name of class to use when unarmshalling | |
contentTypeFormat |
|
| Defines a content type format in which protobuf message will be serialized/deserialized from(to) the Java been. The format can either be native or json for either native protobuf or json fields representation. The default value is native. |
contentTypeHeader |
|
| Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc. |
247.3. Content type format (starting from Camel 2.19) 复制链接链接已复制到粘贴板!
It’s possible to parse JSON message to convert it to the protobuf format and unparse it back using native util converter. To use this option, set contentTypeFormat value to 'json' or call protobuf with second parameter. If default instance is not specified, always use native protobuf format. The sample code shows below:
from("direct:marshal") .unmarshal() .protobuf("org.apache.camel.dataformat.protobuf.generated.AddressBookProtos$Person", "json") .to("mock:reverse");
from("direct:marshal")
.unmarshal()
.protobuf("org.apache.camel.dataformat.protobuf.generated.AddressBookProtos$Person", "json")
.to("mock:reverse");
247.4. Protobuf overview 复制链接链接已复制到粘贴板!
This quick overview of how to use Protobuf. For more detail see the complete tutorial
247.5. Defining the proto format 复制链接链接已复制到粘贴板!
The first step is to define the format for the body of your exchange. This is defined in a .proto file as so:
addressbook.proto
247.6. Generating Java classes 复制链接链接已复制到粘贴板!
The Protobuf SDK provides a compiler which will generate the Java classes for the format we defined in our .proto file. If your operating system is supporting by Protobuf Java code generator maven plugin, you can automate protobuf Java code generating by adding following configurations to your pom.xml:
Insert operating system and CPU architecture detection extension inside <build> tag of the project pom.xml or set ${os.detected.classifier} parameter manually
Insert gRPC and protobuf Java code generator plugin <plugins> tag of the project pom.xml
You can also run the compiler for any additional supported languages you require manually.
protoc --java_out=. ./proto/addressbook.proto
This will generate a single Java class named AddressBookProtos which contains inner classes for Person and AddressBook. Builders are also implemented for you. The generated classes implement com.google.protobuf.Message which is required by the serialization mechanism. For this reason it important that only these classes are used in the body of your exchanges. Camel will throw an exception on route creation if you attempt to tell the Data Format to use a class that does not implement com.google.protobuf.Message. Use the generated builders to translate the data from any of your existing domain classes.
247.7. Java DSL 复制链接链接已复制到粘贴板!
You can use create the ProtobufDataFormat instance and pass it to Camel DataFormat marshal and unmarshal API like this.
ProtobufDataFormat format = new ProtobufDataFormat(Person.getDefaultInstance()); from("direct:in").marshal(format); from("direct:back").unmarshal(format).to("mock:reverse");
ProtobufDataFormat format = new ProtobufDataFormat(Person.getDefaultInstance());
from("direct:in").marshal(format);
from("direct:back").unmarshal(format).to("mock:reverse");
Or use the DSL protobuf() passing the unmarshal default instance or default instance class name like this.
247.8. Spring DSL 复制链接链接已复制到粘贴板!
The following example shows how to use Protobuf to unmarshal using Spring configuring the protobuf data type
247.9. Dependencies 复制链接链接已复制到粘贴板!
To use Protobuf in your camel routes you need to add the a dependency on camel-protobuf which implements this data format.