Class ProtobufJsonDecoder
java.lang.Object
org.springframework.http.codec.protobuf.ProtobufJsonDecoder
- All Implemented Interfaces:
Decoder<com.google.protobuf.Message>
A
Decoder that reads a JSON byte stream and converts it to
Google Protocol Buffers
Messages.
Flux deserialized via
decode(Publisher, ResolvableType, MimeType, Map) are not supported because
the Protobuf Java Util library does not provide a non-blocking parser
that splits a JSON stream into tokens.
Applications should consider decoding to Mono<Message> or
Mono<List<Message>>, which will use the supported
decodeToMono(Publisher, ResolvableType, MimeType, Map).
To generate Message Java classes, you need to install the
protoc binary.
This decoder requires Protobuf 3.29 or higher, and supports
"application/json" and "application/*+json" with
the official "com.google.protobuf:protobuf-java-util" library.
- Since:
- 6.2
- Author:
- Brian Clozel
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intThe default max size for aggregating messages. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a newProtobufJsonDecoderusing a defaultJsonFormat.Parserinstance.ProtobufJsonDecoder(com.google.protobuf.util.JsonFormat.Parser parser) Construct a newProtobufJsonDecoderusing the givenJsonFormat.Parserinstance. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanDecode(ResolvableType elementType, @Nullable 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(Publisher<DataBuffer> inputStream, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Decode aDataBufferinput stream into a Flux ofT.com.google.protobuf.Messagedecode(DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Decode a data buffer to an Object of type T.reactor.core.publisher.Mono<com.google.protobuf.Message> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Decode aDataBufferinput stream into a Mono ofT.Return the list of MIME types supported by this Decoder.intReturn theconfiguredmessage size limit.voidsetMaxMessageSize(int maxMessageSize) The max size allowed per message.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface 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
-
ProtobufJsonDecoder
public ProtobufJsonDecoder()Construct a newProtobufJsonDecoderusing a defaultJsonFormat.Parserinstance. -
ProtobufJsonDecoder
public ProtobufJsonDecoder(com.google.protobuf.util.JsonFormat.Parser parser) Construct a newProtobufJsonDecoderusing the givenJsonFormat.Parserinstance.
-
-
Method Details
-
getMaxMessageSize
public int getMaxMessageSize()Return theconfiguredmessage size limit. -
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
-
canDecode
Description copied from interface:DecoderWhether the decoder supports the given target element type and the MIME type of the source stream. -
getDecodableMimeTypes
Description copied from interface:DecoderReturn the list of MIME types supported by this Decoder. The list may not apply to every possible target element type and calls to this method should typically be guarded viacanDecode(elementType, null). The list may also exclude MIME types supported only for a specific element type. Alternatively, useDecoder.getDecodableMimeTypes(ResolvableType)for a more precise list.- Specified by:
getDecodableMimeTypesin interfaceDecoder<com.google.protobuf.Message>- Returns:
- the list of supported MIME types
-
decode
public reactor.core.publisher.Flux<com.google.protobuf.Message> decode(Publisher<DataBuffer> inputStream, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Description copied from interface:DecoderDecode aDataBufferinput stream into a Flux ofT.- Specified by:
decodein interfaceDecoder<com.google.protobuf.Message>- Parameters:
inputStream- theDataBufferinput stream to decodetargetType- the expected type of elements in the output stream; this type must have been previously passed to theDecoder.canDecode(ResolvableType, MimeType)method and it must have returnedtrue.mimeType- the MIME type associated with the input stream (optional)hints- additional information about how to do decode- Returns:
- the output stream with decoded elements
-
decode
public com.google.protobuf.Message decode(DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) throws DecodingExceptionDescription copied from interface:DecoderDecode 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:
decodein interfaceDecoder<com.google.protobuf.Message>- Parameters:
dataBuffer- theDataBufferto decodetargetType- the expected output typemimeType- the MIME type associated with the datahints- additional information about how to do decode- Returns:
- the decoded value, possibly
null - Throws:
DecodingException
-
decodeToMono
public reactor.core.publisher.Mono<com.google.protobuf.Message> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Description copied from interface:DecoderDecode aDataBufferinput stream into a Mono ofT.- Specified by:
decodeToMonoin interfaceDecoder<com.google.protobuf.Message>- Parameters:
inputStream- theDataBufferinput stream to decodeelementType- the expected type of elements in the output stream; this type must have been previously passed to theDecoder.canDecode(ResolvableType, MimeType)method and it must have returnedtrue.mimeType- the MIME type associated with the input stream (optional)hints- additional information about how to do decode- Returns:
- the output stream with the decoded element
-