Class ProtobufDecoder
- java.lang.Object
- org.springframework.http.codec.protobuf.ProtobufCodecSupport
- org.springframework.http.codec.protobuf.ProtobufDecoder
- All Implemented Interfaces:
Decoder<com.google.protobuf.Message>
public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<com.google.protobuf.Message>
ADecoder
that readsMessage
s 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 viadecodeToMono(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 emptyMono
.To generate
Message
Java classes, you need to install theprotoc
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 Description protected static int
DEFAULT_MESSAGE_MAX_SIZE
The default max size for aggregating messages.
Constructor Summary
Constructors Constructor Description ProtobufDecoder()
Construct a newProtobufDecoder
.ProtobufDecoder(com.google.protobuf.ExtensionRegistry extensionRegistry)
Construct a newProtobufDecoder
with an initializer that allows the registration of message extensions.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canDecode(ResolvableType elementType, MimeType mimeType)
Whether the decoder supports the given target element type and the MIME type of the source stream.reactor.core.publisher.Flux<com.google.protobuf.Message>
decode(org.reactivestreams.Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String,Object> hints)
Decode aDataBuffer
input stream into a Flux ofT
.com.google.protobuf.Message
decode(DataBuffer dataBuffer, ResolvableType targetType, MimeType mimeType, Map<String,Object> hints)
Decode a data buffer to an Object of type T.reactor.core.publisher.Mono<com.google.protobuf.Message>
decodeToMono(org.reactivestreams.Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String,Object> hints)
Decode aDataBuffer
input stream into a Mono ofT
.List<MimeType>
getDecodableMimeTypes()
Return the list of MIME types this decoder supports.int
getMaxMessageSize()
Return theconfigured
message size limit.void
setMaxMessageSize(int maxMessageSize)
The max size allowed per message.Methods inherited from class org.springframework.http.codec.protobuf.ProtobufCodecSupport
getMimeTypes, supportsMimeType
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 newProtobufDecoder
.
ProtobufDecoder
public ProtobufDecoder(com.google.protobuf.ExtensionRegistry extensionRegistry)
Construct a newProtobufDecoder
with an initializer that allows the registration of message extensions.- Parameters:
extensionRegistry
- a message extension registry
Method Detail
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
public int getMaxMessageSize()
Return theconfigured
message size limit.- Since:
- 5.1.11
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.
decode
public reactor.core.publisher.Flux<com.google.protobuf.Message> decode(org.reactivestreams.Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String,Object> hints)
Description copied from interface:Decoder
Decode aDataBuffer
input stream into a Flux ofT
.- Specified by:
decode
in interfaceDecoder<com.google.protobuf.Message>
- Parameters:
inputStream
- theDataBuffer
input stream to decodeelementType
- the expected type of elements in the output stream; this type must have been previously passed to theDecoder.canDecode(org.springframework.core.ResolvableType, org.springframework.util.MimeType)
method and it must have returnedtrue
.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 Map<String,Object> hints)
Description copied from interface:Decoder
Decode aDataBuffer
input stream into a Mono ofT
.- Specified by:
decodeToMono
in interfaceDecoder<com.google.protobuf.Message>
- Parameters:
inputStream
- theDataBuffer
input stream to decodeelementType
- the expected type of elements in the output stream; this type must have been previously passed to theDecoder.canDecode(org.springframework.core.ResolvableType, org.springframework.util.MimeType)
method and it must have returnedtrue
.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
decode
public com.google.protobuf.Message decode(DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String,Object> hints) throws DecodingException
Description copied from interface:Decoder
Decode a data buffer to an Object of type T. This is useful for scenarios, that distinct messages (or events) are decoded and handled individually, in fully aggregated form.- Specified by:
decode
in interfaceDecoder<com.google.protobuf.Message>
- Parameters:
dataBuffer
- theDataBuffer
to decodetargetType
- the expected output typemimeType
- the MIME type associated with the datahints
- additional information about how to do encode- Returns:
- the decoded value, possibly
null
- Throws:
DecodingException
getDecodableMimeTypes
public List<MimeType> getDecodableMimeTypes()
Description copied from interface:Decoder
Return the list of MIME types this decoder supports.- Specified by:
getDecodableMimeTypes
in interfaceDecoder<com.google.protobuf.Message>