public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Message>
Decoder
that reads com.google.protobuf.Message
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 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.
ProtobufEncoder
Modifier and Type | Class and Description |
---|---|
private class |
ProtobufDecoder.MessageDecoderFunction |
Modifier and Type | Field and Description |
---|---|
protected static int |
DEFAULT_MESSAGE_MAX_SIZE
The default max size for aggregating messages.
|
private ExtensionRegistry |
extensionRegistry |
private int |
maxMessageSize |
private static java.util.concurrent.ConcurrentMap<java.lang.Class<?>,java.lang.reflect.Method> |
methodCache |
DELIMITED_KEY, DELIMITED_VALUE, MIME_TYPES
Constructor and Description |
---|
ProtobufDecoder()
Construct a new
ProtobufDecoder . |
ProtobufDecoder(ExtensionRegistry extensionRegistry)
Construct a new
ProtobufDecoder with an initializer that allows the
registration of message extensions. |
Modifier and Type | Method and Description |
---|---|
boolean |
canDecode(ResolvableType elementType,
MimeType mimeType)
Whether the decoder supports the given target element type and the MIME
type of the source stream.
|
<any> |
decode(<any> inputStream,
ResolvableType elementType,
MimeType mimeType,
java.util.Map<java.lang.String,java.lang.Object> hints)
Decode a
DataBuffer input stream into a Flux of T . |
<any> |
decodeToMono(<any> inputStream,
ResolvableType elementType,
MimeType mimeType,
java.util.Map<java.lang.String,java.lang.Object> hints)
Decode a
DataBuffer input stream into a Mono of T . |
java.util.List<MimeType> |
getDecodableMimeTypes()
Return the list of MIME types this decoder supports.
|
private static Message.Builder |
getMessageBuilder(java.lang.Class<?> clazz)
Create a new
Message.Builder instance for the given class. |
void |
setMaxMessageSize(int maxMessageSize) |
getMimeTypes, supportsMimeType
protected static final int DEFAULT_MESSAGE_MAX_SIZE
private static final java.util.concurrent.ConcurrentMap<java.lang.Class<?>,java.lang.reflect.Method> methodCache
private final ExtensionRegistry extensionRegistry
private int maxMessageSize
public ProtobufDecoder()
ProtobufDecoder
.public ProtobufDecoder(ExtensionRegistry extensionRegistry)
ProtobufDecoder
with an initializer that allows the
registration of message extensions.extensionRegistry
- a message extension registrypublic void setMaxMessageSize(int maxMessageSize)
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType)
Decoder
public <any> decode(<any> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable java.util.Map<java.lang.String,java.lang.Object> hints)
Decoder
DataBuffer
input stream into a Flux of T
.decode
in interface Decoder<Message>
inputStream
- the DataBuffer
input stream to decodeelementType
- 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 encodepublic <any> decodeToMono(<any> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable java.util.Map<java.lang.String,java.lang.Object> hints)
Decoder
DataBuffer
input stream into a Mono of T
.decodeToMono
in interface Decoder<Message>
inputStream
- the DataBuffer
input stream to decodeelementType
- 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 encodeprivate static Message.Builder getMessageBuilder(java.lang.Class<?> clazz) throws java.lang.Exception
Message.Builder
instance for the given class.
This method uses a ConcurrentHashMap for caching method lookups.
java.lang.Exception
public java.util.List<MimeType> getDecodableMimeTypes()
Decoder
getDecodableMimeTypes
in interface Decoder<Message>