Class ProtobufDecoder
- All Implemented Interfaces:
org.springframework.core.codec.Decoder<com.google.protobuf.Message>
Decoder that reads Messages using
Google Protocol Buffers.
Flux deserialized via
decode(Publisher, ResolvableType, MimeType, Map) are expected to use
delimited Protobuf messages with the size of each message specified before
the message itself. Single values deserialized via
decodeToMono(Publisher, ResolvableType, MimeType, Map) are expected
to use regular Protobuf message format (without the size prepended before
the message).
Notice that default instance of Protobuf message produces empty byte
array, so Mono.just(Msg.getDefaultInstance()) sent over the network
will be deserialized as an empty Mono.
To generate Message Java classes, you need to install the
protoc binary.
This decoder requires Protobuf 3 or higher, and supports
"application/x-protobuf" and "application/octet-stream" with
the official "com.google.protobuf:protobuf-java" library.
- Since:
- 5.1
- Author:
- Sebastien Deleuze
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceComponent to read the size of a message. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intThe default max size for aggregating messages.Fields inherited from class ProtobufCodecSupport
MIME_TYPES -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a newProtobufDecoder.ProtobufDecoder(com.google.protobuf.ExtensionRegistry extensionRegistry) Construct a newProtobufDecoderwith an initializer that allows the registration of message extensions. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanDecode(org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType) reactor.core.publisher.Flux<com.google.protobuf.Message> decode(Publisher<org.springframework.core.io.buffer.DataBuffer> inputStream, org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String, Object> hints) com.google.protobuf.Messagedecode(org.springframework.core.io.buffer.DataBuffer dataBuffer, org.springframework.core.ResolvableType targetType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String, Object> hints) reactor.core.publisher.Mono<com.google.protobuf.Message> decodeToMono(Publisher<org.springframework.core.io.buffer.DataBuffer> inputStream, org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String, Object> hints) List<org.springframework.util.MimeType> intReturn theconfiguredmessage size limit.protected ProtobufDecoder.MessageSizeReaderReturn a reader for message size information encoded in the input stream.protected voidmerge(org.springframework.core.io.buffer.DataBuffer dataBuffer, com.google.protobuf.Message.Builder builder) Use merge methods onMessage.Builderto read a single message from the givenDataBuffer.voidsetMaxMessageSize(int maxMessageSize) The max size allowed per message.Methods inherited from class ProtobufCodecSupport
getMimeTypes, setMimeTypes, supportsMimeTypeMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.core.codec.Decoder
getDecodableMimeTypes
-
Field Details
-
DEFAULT_MESSAGE_MAX_SIZE
protected static final int DEFAULT_MESSAGE_MAX_SIZEThe default max size for aggregating messages.- See Also:
-
-
Constructor Details
-
ProtobufDecoder
public ProtobufDecoder()Construct a newProtobufDecoder. -
ProtobufDecoder
public ProtobufDecoder(com.google.protobuf.ExtensionRegistry extensionRegistry) Construct a newProtobufDecoderwith an initializer that allows the registration of message extensions.- Parameters:
extensionRegistry- a message extension registry
-
-
Method Details
-
setMaxMessageSize
public void setMaxMessageSize(int maxMessageSize) The max size allowed per message.By default, this is set to 256K.
- Parameters:
maxMessageSize- the max size per message, or -1 for unlimited
-
getMaxMessageSize
-
canDecode
public boolean canDecode(org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType) - Specified by:
canDecodein interfaceorg.springframework.core.codec.Decoder<com.google.protobuf.Message>
-
decode
public reactor.core.publisher.Flux<com.google.protobuf.Message> decode(Publisher<org.springframework.core.io.buffer.DataBuffer> inputStream, org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String, Object> hints) - Specified by:
decodein interfaceorg.springframework.core.codec.Decoder<com.google.protobuf.Message>
-
initMessageSizeReader
Return a reader for message size information encoded in the input stream.- Since:
- 7.0
-
decodeToMono
public reactor.core.publisher.Mono<com.google.protobuf.Message> decodeToMono(Publisher<org.springframework.core.io.buffer.DataBuffer> inputStream, org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String, Object> hints) - Specified by:
decodeToMonoin interfaceorg.springframework.core.codec.Decoder<com.google.protobuf.Message>
-
decode
public com.google.protobuf.Message decode(org.springframework.core.io.buffer.DataBuffer dataBuffer, org.springframework.core.ResolvableType targetType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String, Object> hints) throws org.springframework.core.codec.DecodingException- Specified by:
decodein interfaceorg.springframework.core.codec.Decoder<com.google.protobuf.Message>- Throws:
org.springframework.core.codec.DecodingException
-
merge
protected void merge(org.springframework.core.io.buffer.DataBuffer dataBuffer, com.google.protobuf.Message.Builder builder) throws IOException Use merge methods onMessage.Builderto read a single message from the givenDataBuffer.- Throws:
IOException- Since:
- 7.0
-
getDecodableMimeTypes
- Specified by:
getDecodableMimeTypesin interfaceorg.springframework.core.codec.Decoder<com.google.protobuf.Message>
-