org.springframework.http.codec.protobuf

Class ProtobufDecoder

  • All Implemented Interfaces:
    Decoder<com.google.protobuf.Message>


    public class ProtobufDecoder
    extends ProtobufCodecSupport
    implements Decoder<com.google.protobuf.Message>
    A 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:
    S├ębastien Deleuze
    See Also:
    ProtobufEncoder
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      protected static int DEFAULT_MESSAGE_MAX_SIZE
      The default max size for aggregating messages.
    • Constructor Summary

      Constructors 
      Constructor and Description
      ProtobufDecoder()
      Construct a new ProtobufDecoder.
      ProtobufDecoder(com.google.protobuf.ExtensionRegistry extensionRegistry)
      Construct a new ProtobufDecoder with an initializer that allows the registration of message extensions.
    • Field Detail

      • DEFAULT_MESSAGE_MAX_SIZE

        protected static final int DEFAULT_MESSAGE_MAX_SIZE
        The default max size for aggregating messages.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ProtobufDecoder

        public ProtobufDecoder()
        Construct a new ProtobufDecoder.
      • ProtobufDecoder

        public ProtobufDecoder(com.google.protobuf.ExtensionRegistry extensionRegistry)
        Construct a new ProtobufDecoder with an initializer that allows the registration of message extensions.
        Parameters:
        extensionRegistry - a message extension registry
    • Method Detail

      • setMaxMessageSize

        public void setMaxMessageSize(int maxMessageSize)
      • canDecode

        public boolean canDecode(ResolvableType elementType,
                                 @Nullable
                                 MimeType mimeType)
        Description copied from interface: Decoder
        Whether the decoder supports the given target element type and the MIME type of the source stream.
        Specified by:
        canDecode in interface Decoder<com.google.protobuf.Message>
        Parameters:
        elementType - the target element type for the output stream
        mimeType - the mime type associated with the stream to decode (can be null if not specified)
        Returns:
        true if supported, false otherwise
      • decode

        public reactor.core.publisher.Flux<com.google.protobuf.Message> decode(org.reactivestreams.Publisher<DataBuffer> inputStream,
                                                                               ResolvableType elementType,
                                                                               @Nullable
                                                                               MimeType mimeType,
                                                                               @Nullable
                                                                               java.util.Map<java.lang.String,java.lang.Object> hints)
        Description copied from interface: Decoder
        Decode a DataBuffer input stream into a Flux of T.
        Specified by:
        decode in interface Decoder<com.google.protobuf.Message>
        Parameters:
        inputStream - the DataBuffer input stream to decode
        elementType - the expected type of elements in the output stream; this type must have been previously passed to the Decoder.canDecode(org.springframework.core.ResolvableType, org.springframework.util.MimeType) method and it must have returned true.
        mimeType - the MIME type associated with the input stream (optional)
        hints - additional information about how to do encode
        Returns:
        the output stream with decoded elements
      • decodeToMono

        public reactor.core.publisher.Mono<com.google.protobuf.Message> decodeToMono(org.reactivestreams.Publisher<DataBuffer> inputStream,
                                                                                     ResolvableType elementType,
                                                                                     @Nullable
                                                                                     MimeType mimeType,
                                                                                     @Nullable
                                                                                     java.util.Map<java.lang.String,java.lang.Object> hints)
        Description copied from interface: Decoder
        Decode a DataBuffer input stream into a Mono of T.
        Specified by:
        decodeToMono in interface Decoder<com.google.protobuf.Message>
        Parameters:
        inputStream - the DataBuffer input stream to decode
        elementType - the expected type of elements in the output stream; this type must have been previously passed to the Decoder.canDecode(org.springframework.core.ResolvableType, org.springframework.util.MimeType) method and it must have returned true.
        mimeType - the MIME type associated with the input stream (optional)
        hints - additional information about how to do encode
        Returns:
        the output stream with the decoded element
      • getDecodableMimeTypes

        public java.util.List<MimeType> getDecodableMimeTypes()
        Description copied from interface: Decoder
        Return the list of MIME types this decoder supports.
        Specified by:
        getDecodableMimeTypes in interface Decoder<com.google.protobuf.Message>