Class ProtobufJsonDecoder

java.lang.Object
org.springframework.http.codec.protobuf.ProtobufJsonDecoder
All Implemented Interfaces:
org.springframework.core.codec.Decoder<com.google.protobuf.Message>

public class ProtobufJsonDecoder extends Object implements org.springframework.core.codec.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

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

    Constructors
    Constructor
    Description
    Construct a new ProtobufJsonDecoder using a default JsonFormat.Parser instance.
    ProtobufJsonDecoder(com.google.protobuf.util.JsonFormat.Parser parser)
    Construct a new ProtobufJsonDecoder using the given JsonFormat.Parser instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canDecode(org.springframework.core.ResolvableType elementType, 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 targetType, org.springframework.util.MimeType mimeType, Map<String,Object> hints)
     
    com.google.protobuf.Message
    decode(org.springframework.core.io.buffer.DataBuffer dataBuffer, org.springframework.core.ResolvableType targetType, org.springframework.util.MimeType mimeType, 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, org.springframework.util.MimeType mimeType, Map<String,Object> hints)
     
    List<org.springframework.util.MimeType>
     
    int
    Return the configured message size limit.
    void
    setMaxMessageSize(int maxMessageSize)
    The max size allowed per message.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.core.codec.Decoder

    getDecodableMimeTypes
  • Field Details

    • DEFAULT_MESSAGE_MAX_SIZE

      protected static final int DEFAULT_MESSAGE_MAX_SIZE
      The default max size for aggregating messages.
      See Also:
  • Constructor Details

    • ProtobufJsonDecoder

      public ProtobufJsonDecoder()
      Construct a new ProtobufJsonDecoder using a default JsonFormat.Parser instance.
    • ProtobufJsonDecoder

      public ProtobufJsonDecoder(com.google.protobuf.util.JsonFormat.Parser parser)
      Construct a new ProtobufJsonDecoder using the given JsonFormat.Parser instance.
  • Method Details

    • getMaxMessageSize

      public int getMaxMessageSize()
      Return the configured message 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

      public boolean canDecode(org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType)
      Specified by:
      canDecode in interface org.springframework.core.codec.Decoder<com.google.protobuf.Message>
    • getDecodableMimeTypes

      public List<org.springframework.util.MimeType> getDecodableMimeTypes()
      Specified by:
      getDecodableMimeTypes in interface org.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 targetType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String,Object> hints)
      Specified by:
      decode in interface org.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:
      decode in interface org.springframework.core.codec.Decoder<com.google.protobuf.Message>
      Throws:
      org.springframework.core.codec.DecodingException
    • 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:
      decodeToMono in interface org.springframework.core.codec.Decoder<com.google.protobuf.Message>