Package org.springframework.http.codec.protobuf

# Class ProtobufEncoder

java.lang.Object
org.springframework.http.codec.protobuf.ProtobufCodecSupport
org.springframework.http.codec.protobuf.ProtobufEncoder

All Implemented Interfaces:
  `org.springframework.core.codec.Encoder<com.google.protobuf.Message>`, `HttpMessageEncoder<com.google.protobuf.Message>`

public class ProtobufEncoder extends ProtobufCodecSupport implements HttpMessageEncoder<com.google.protobuf.Message>
An `Encoder` that writes `Message`s using Google Protocol Buffers.

Flux are serialized using delimited Protobuf messages with the size of each message specified before the message itself. Single values are serialized using regular Protobuf message format (without the size prepended before the message).

To generate `Message` Java classes, you need to install the `protoc` binary.

This encoder requires Protobuf 3.29 or higher, and supports `"application/x-protobuf"` and `"application/octet-stream"` with the official `"com.google.protobuf:protobuf-java"` library.

Since:
  5.1
Author:
  Sebastien Deleuze
See Also:
  - `ProtobufDecoder`

## Constructor Summary

Constructors
Constructor
Description
`ProtobufEncoder()`

## Method Summary

Modifier and Type
Method
Description
`boolean`
`canEncode(org.springframework.core.ResolvableType elementType, org.springframework.util.MimeType mimeType)`
`reactor.core.publisher.Flux<org.springframework.core.io.buffer.DataBuffer>`
`encode(Publisher<? extends com.google.protobuf.Message> inputStream, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType elementType, org.springframework.util.MimeType mimeType, Map<String,Object> hints)`
`org.springframework.core.io.buffer.DataBuffer`
`encodeValue(com.google.protobuf.Message message, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType valueType, org.springframework.util.MimeType mimeType, Map<String,Object> hints)`
`List<org.springframework.util.MimeType>`
`getEncodableMimeTypes()`
`List<MediaType>`
`getStreamingMediaTypes()`
Return "streaming" media types for which flushing should be performed automatically vs at the end of the input stream.

### Methods inherited from class org.springframework.http.codec.protobuf.ProtobufCodecSupport

`getMimeTypes, supportsMimeType`

### 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.Encoder

`getEncodableMimeTypes`

### Methods inherited from interface org.springframework.http.codec.HttpMessageEncoder

`getEncodeHints`

## Constructor Details

### ProtobufEncoder

public ProtobufEncoder()

## Method Details

### canEncode

public boolean canEncode(org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType)

Specified by:
  `canEncode` in interface `org.springframework.core.codec.Encoder<com.google.protobuf.Message>`

### encode

public reactor.core.publisher.Flux<org.springframework.core.io.buffer.DataBuffer> encode(Publisher<? extends com.google.protobuf.Message> inputStream, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String,Object> hints)

Specified by:
  `encode` in interface `org.springframework.core.codec.Encoder<com.google.protobuf.Message>`

### encodeValue

public org.springframework.core.io.buffer.DataBuffer encodeValue(com.google.protobuf.Message message, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType valueType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String,Object> hints)

Specified by:
  `encodeValue` in interface `org.springframework.core.codec.Encoder<com.google.protobuf.Message>`

### getStreamingMediaTypes

public List<MediaType> getStreamingMediaTypes()
Description copied from interface: `HttpMessageEncoder`
Return "streaming" media types for which flushing should be performed automatically vs at the end of the input stream.

Specified by:
  `getStreamingMediaTypes` in interface `HttpMessageEncoder<com.google.protobuf.Message>`

### getEncodableMimeTypes

public List<org.springframework.util.MimeType> getEncodableMimeTypes()

Specified by:
  `getEncodableMimeTypes` in interface `org.springframework.core.codec.Encoder<com.google.protobuf.Message>`