265.7. Java DSL

您可以使用 create ProtobufDataFormat 实例并将它传递到 Camel DataFormat marshal 和 unmarshal API。

   ProtobufDataFormat format = new ProtobufDataFormat(Person.getDefaultInstance());

   from("direct:in").marshal(format);
   from("direct:back").unmarshal(format).to("mock:reverse");

或者,使用 DSL protobuf ()传递 unmarshal default 实例或默认实例类名称,如下所示。

   // You don't need to specify the default instance for protobuf marshaling
   from("direct:marshal").marshal().protobuf();
   from("direct:unmarshalA").unmarshal()
       .protobuf("org.apache.camel.dataformat.protobuf.generated.AddressBookProtos$Person")
       .to("mock:reverse");

   from("direct:unmarshalB").unmarshal().protobuf(Person.getDefaultInstance()).to("mock:reverse");