Top Description Inners Fields Constructors Methods
org.springframework.http.converter.protobuf

public Class ProtobufHttpMessageConverter

extends AbstractHttpMessageConverter<Message>
Class Inheritance
Known Direct Subclasses
org.springframework.http.converter.protobuf.ProtobufJsonFormatHttpMessageConverter
Imports
java.io.IOException, .InputStream, .InputStreamReader, .OutputStream, .OutputStreamWriter, java.lang.reflect.Method, java.nio.charset.Charset, .StandardCharsets, java.util.Arrays, .Map, com.google.protobuf.CodedOutputStream, .ExtensionRegistry, .Message, .TextFormat, com.google.protobuf.util.JsonFormat, org.springframework.http.HttpInputMessage, .HttpOutputMessage, .MediaType, org.springframework.http.converter.AbstractHttpMessageConverter, .HttpMessageConversionException, .HttpMessageNotReadableException, .HttpMessageNotWritableException, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .ConcurrentReferenceHashMap

An HttpMessageConverter that reads and writes com.google.protobuf.Messages using Google Protocol Buffers.

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

This converter supports by default "application/x-protobuf" and "text/plain" with the official "com.google.protobuf:protobuf-java" library. The "application/json" format is also supported with the "com.google.protobuf:protobuf-java-util" dependency. See ProtobufJsonFormatHttpMessageConverter for a configurable variant.

This converter requires Protobuf 3 or higher as of Spring Framework 6.1.

Authors
Alex Antonov, Brian Clozel, Juergen Hoeller, Sebastien Deleuze
Since
4.1
See Also
JsonFormat, ProtobufJsonFormatHttpMessageConverter

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static interface
pack-priv static class
ProtobufHttpMessageConverter.ProtobufJavaUtilSupport

ProtobufFormatSupport implementation used when com.google.protobuf.util.JsonFormat is available.

Field Summary

Modifier and TypeField and Description
public static final Charset
DEFAULT_CHARSET

The default charset used by the converter.

pack-priv final ExtensionRegistry
private static final Map<Class<?>, Method>
public static final MediaType
PROTOBUF

The media-type for protobuf application/x-protobuf.

private final ProtobufHttpMessageConverter.ProtobufFormatSupport
private static final boolean
public static final String
X_PROTOBUF_MESSAGE_HEADER

The HTTP header containing the protobuf message.

public static final String
X_PROTOBUF_SCHEMA_HEADER

The HTTP header containing the protobuf schema.

Inherited from org.springframework.http.converter.AbstractHttpMessageConverter:
logger

Constructor Summary

AccessConstructor and Description
public
ProtobufHttpMessageConverter()

Construct a new ProtobufHttpMessageConverter.

public
ProtobufHttpMessageConverter(ExtensionRegistry
the registry to populate
extensionRegistry
)

Construct a new ProtobufHttpMessageConverter with a registry that specifies protocol message extensions.

pack-priv

Method Summary

Modifier and TypeMethod and Description
protected boolean
canWrite(MediaType
the media type to write, can be null if not specified. Typically the value of an Accept header.
mediaType
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canWrite.

Returns true if the given media type includes any of the supported media types.

protected MediaType
getDefaultContentType(Message
the type to return the content type for
message
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getDefaultContentType.

Returns the default content type for the given type.

private Message.Builder
getMessageBuilder(Class<? extends Message> clazz)

Create a new Message.Builder instance for the given class.

protected Message
readInternal(Class<? extends Message>
the type of object to return
clazz
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Abstract template method that reads the actual object.

private void
setProtoHeader(HttpOutputMessage response, Message message)

Set the "X-Protobuf-*" HTTP headers when responding with a message of content type "application/x-protobuf"

Note

outputMessage.getBody() should not have been called before because it writes HTTP headers (making them read only).

protected boolean
supports(Class<?>
the class to test for support
clazz
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates whether the given class is supported by this converter.

protected boolean
supportsRepeatableWrites(Message
the object t
message
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Indicates whether this message converter can write the given object multiple times.

protected void
writeInternal(Message
the object to write to the output message
message
,
HttpOutputMessage
the HTTP output message to write to
outputMessage
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Abstract template method that writes the actual body.

Inherited from org.springframework.http.converter.AbstractHttpMessageConverter:
addDefaultHeaderscanReadcanReadcanWritegetContentLengthgetDefaultCharsetgetSupportedMediaTypesreadsetDefaultCharsetsetSupportedMediaTypeswrite

Field Detail

DEFAULT_CHARSETback to summary
public static final Charset DEFAULT_CHARSET

The default charset used by the converter.

extensionRegistryback to summary
pack-priv final ExtensionRegistry extensionRegistry
methodCacheback to summary
private static final Map<Class<?>, Method> methodCache
PROTOBUFback to summary
public static final MediaType PROTOBUF

The media-type for protobuf application/x-protobuf.

protobufFormatSupportback to summary
private final ProtobufHttpMessageConverter.ProtobufFormatSupport protobufFormatSupport
Annotations
@Nullable
protobufJsonFormatPresentback to summary
private static final boolean protobufJsonFormatPresent
X_PROTOBUF_MESSAGE_HEADERback to summary
public static final String X_PROTOBUF_MESSAGE_HEADER

The HTTP header containing the protobuf message.

X_PROTOBUF_SCHEMA_HEADERback to summary
public static final String X_PROTOBUF_SCHEMA_HEADER

The HTTP header containing the protobuf schema.

Constructor Detail

ProtobufHttpMessageConverterback to summary
public ProtobufHttpMessageConverter()

Construct a new ProtobufHttpMessageConverter.

ProtobufHttpMessageConverterback to summary
public ProtobufHttpMessageConverter(ExtensionRegistry extensionRegistry)

Construct a new ProtobufHttpMessageConverter with a registry that specifies protocol message extensions.

Parameters
extensionRegistry:ExtensionRegistry

the registry to populate

ProtobufHttpMessageConverterback to summary
pack-priv ProtobufHttpMessageConverter(ProtobufHttpMessageConverter.ProtobufFormatSupport formatSupport, ExtensionRegistry extensionRegistry)

Method Detail

canWriteback to summary
protected boolean canWrite(MediaType mediaType)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canWrite.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.canWrite.

Returns true if the given media type includes any of the supported media types.

Parameters
mediaType:MediaType

the media type to write, can be null if not specified. Typically the value of an Accept header.

Returns:boolean

true if the supported media types are compatible with the media type, or if the media type is null

Annotations
@Override
getDefaultContentTypeback to summary
protected MediaType getDefaultContentType(Message message)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getDefaultContentType.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.getDefaultContentType.

Returns the default content type for the given type. Called when write is invoked without a specified content type parameter.

By default, this returns the first element of the supportedMediaTypes property, if any. Can be overridden in subclasses.

Parameters
message:Message

the type to return the content type for

Returns:MediaType

the content type, or null if not known

Annotations
@Override
getMessageBuilderback to summary
private Message.Builder getMessageBuilder(Class<? extends Message> clazz)

Create a new Message.Builder instance for the given class.

This method uses a ConcurrentReferenceHashMap for caching method lookups.

readInternalback to summary
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Abstract template method that reads the actual object. Invoked from read.

Parameters
clazz:Class<? extends Message>

the type of object to return

inputMessage:HttpInputMessage

the HTTP input message to read from

Returns:Message

the converted object

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
HttpMessageNotReadableException:
in case of conversion errors
setProtoHeaderback to summary
private void setProtoHeader(HttpOutputMessage response, Message message)

Set the "X-Protobuf-*" HTTP headers when responding with a message of content type "application/x-protobuf"

Note

outputMessage.getBody() should not have been called before because it writes HTTP headers (making them read only).

supportsback to summary
protected boolean supports(Class<?> clazz)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates whether the given class is supported by this converter.

Parameters
clazz:Class<?>

the class to test for support

Returns:boolean

true if supported; false otherwise

Annotations
@Override
supportsRepeatableWritesback to summary
protected boolean supportsRepeatableWrites(Message message)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Indicates whether this message converter can write the given object multiple times.

Default implementation returns false.

Parameters
message:Message

the object t

Returns:boolean

true if t can be written repeatedly; false otherwise

Annotations
@Override
writeInternalback to summary
protected void writeInternal(Message message, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Abstract template method that writes the actual body. Invoked from write.

Parameters
message:Message

the object to write to the output message

outputMessage:HttpOutputMessage

the HTTP output message to write to

Annotations
@SuppressWarnings:deprecation
@Override
Exceptions
IOException:
in case of I/O errors
HttpMessageNotWritableException:
in case of conversion errors
org.springframework.http.converter.protobuf back to summary

pack-priv Interface ProtobufHttpMessageConverter.ProtobufFormatSupport

Known Direct Implementers
org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter.ProtobufJavaUtilSupport

Protobuf format support.

Method Summary

Modifier and TypeMethod and Description
public void
merge(InputStream input, Charset charset, MediaType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder)

public void
print(Message message, OutputStream output, MediaType contentType, Charset charset)

public MediaType[]
public boolean

Method Detail

mergeback to summary
public void merge(InputStream input, Charset charset, MediaType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException, HttpMessageConversionException
printback to summary
public void print(Message message, OutputStream output, MediaType contentType, Charset charset) throws IOException, HttpMessageConversionException
supportedMediaTypesback to summary
public MediaType[] supportedMediaTypes()
supportsWriteOnlyback to summary
public boolean supportsWriteOnly(MediaType mediaType)
org.springframework.http.converter.protobuf back to summary

pack-priv Class ProtobufHttpMessageConverter.ProtobufJavaUtilSupport

extends Object
implements ProtobufFormatSupport
Class Inheritance
  • java.lang.Object
  • org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter.ProtobufJavaUtilSupport
All Implemented Interfaces
org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter.ProtobufFormatSupport

ProtobufFormatSupport implementation used when com.google.protobuf.util.JsonFormat is available.

Field Summary

Modifier and TypeField and Description
private final JsonFormat.Parser
private final JsonFormat.Printer

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
public void
public MediaType[]
public boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

parserback to summary
private final JsonFormat.Parser parser
printerback to summary
private final JsonFormat.Printer printer

Constructor Detail

ProtobufJavaUtilSupportback to summary
public ProtobufJavaUtilSupport(JsonFormat.Parser parser, JsonFormat.Printer printer)

Method Detail

mergeback to summary
public void merge(InputStream input, Charset charset, MediaType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException, HttpMessageConversionException

Implements org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter.ProtobufFormatSupport.merge.

Annotations
@Override
printback to summary
public void print(Message message, OutputStream output, MediaType contentType, Charset charset) throws IOException, HttpMessageConversionException

Implements org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter.ProtobufFormatSupport.print.

Annotations
@Override
supportedMediaTypesback to summary
public MediaType[] supportedMediaTypes()

Implements org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter.ProtobufFormatSupport.supportedMediaTypes.

Annotations
@Override
supportsWriteOnlyback to summary
public boolean supportsWriteOnly(MediaType mediaType)

Implements org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter.ProtobufFormatSupport.supportsWriteOnly.

Annotations
@Override