Class ProtobufHttpMessageConverter
java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<com.google.protobuf.Message>
org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter
- All Implemented Interfaces:
- HttpMessageConverter<com.google.protobuf.Message>
- Direct Known Subclasses:
- ProtobufJsonFormatHttpMessageConverter
public class ProtobufHttpMessageConverter
extends AbstractHttpMessageConverter<com.google.protobuf.Message>
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.
- Since:
- 4.1
- Author:
- Alex Antonov, Brian Clozel, Juergen Hoeller, Sebastien Deleuze
- See Also:
- 
- JsonFormat
- ProtobufJsonFormatHttpMessageConverter
 
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final CharsetThe default charset used by the converter.static final MediaTypeThe media-type for protobufapplication/x-protobuf.static final StringThe HTTP header containing the protobuf message.static final StringThe HTTP header containing the protobuf schema.Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverterlogger
- 
Constructor SummaryConstructorsConstructorDescriptionConstruct a newProtobufHttpMessageConverter.ProtobufHttpMessageConverter(com.google.protobuf.ExtensionRegistry extensionRegistry) Construct a newProtobufHttpMessageConverterwith a registry that specifies protocol message extensions.
- 
Method SummaryModifier and TypeMethodDescriptionprotected booleanReturnstrueif the given media type includes any of the supported media types.protected MediaTypegetDefaultContentType(com.google.protobuf.Message message) Returns the default content type for the given type.protected com.google.protobuf.MessagereadInternal(Class<? extends com.google.protobuf.Message> clazz, HttpInputMessage inputMessage) Abstract template method that reads the actual object.protected booleanIndicates whether the given class is supported by this converter.protected booleansupportsRepeatableWrites(com.google.protobuf.Message message) Indicates whether this message converter can write the given object multiple times.protected voidwriteInternal(com.google.protobuf.Message message, HttpOutputMessage outputMessage) Abstract template method that writes the actual body.Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverteraddDefaultHeaders, canRead, canRead, canWrite, getContentLength, getDefaultCharset, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, writeMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.http.converter.HttpMessageConvertergetSupportedMediaTypes
- 
Field Details- 
DEFAULT_CHARSETThe default charset used by the converter.
- 
PROTOBUFThe media-type for protobufapplication/x-protobuf.
- 
X_PROTOBUF_SCHEMA_HEADERThe HTTP header containing the protobuf schema.- See Also:
 
- 
X_PROTOBUF_MESSAGE_HEADERThe HTTP header containing the protobuf message.- See Also:
 
 
- 
- 
Constructor Details- 
ProtobufHttpMessageConverterpublic ProtobufHttpMessageConverter()Construct a newProtobufHttpMessageConverter.
- 
ProtobufHttpMessageConverterpublic ProtobufHttpMessageConverter(com.google.protobuf.ExtensionRegistry extensionRegistry) Construct a newProtobufHttpMessageConverterwith a registry that specifies protocol message extensions.- Parameters:
- extensionRegistry- the registry to populate
 
 
- 
- 
Method Details- 
supportsDescription copied from class:AbstractHttpMessageConverterIndicates whether the given class is supported by this converter.- Specified by:
- supportsin class- AbstractHttpMessageConverter<com.google.protobuf.Message>
- Parameters:
- clazz- the class to test for support
- Returns:
- trueif supported;- falseotherwise
 
- 
getDefaultContentTypeDescription copied from class:AbstractHttpMessageConverterReturns the default content type for the given type. Called whenAbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)is invoked without a specified content type parameter.By default, this returns the first element of the supportedMediaTypesproperty, if any. Can be overridden in subclasses.- Overrides:
- getDefaultContentTypein class- AbstractHttpMessageConverter<com.google.protobuf.Message>
- Parameters:
- message- the type to return the content type for
- Returns:
- the content type, or nullif not known
 
- 
readInternalprotected com.google.protobuf.Message readInternal(Class<? extends com.google.protobuf.Message> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException Description copied from class:AbstractHttpMessageConverterAbstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage).- Specified by:
- readInternalin class- AbstractHttpMessageConverter<com.google.protobuf.Message>
- Parameters:
- clazz- the type of object to return
- inputMessage- the HTTP input message to read from
- Returns:
- the converted object
- Throws:
- IOException- in case of I/O errors
- HttpMessageNotReadableException- in case of conversion errors
 
- 
canWriteDescription copied from class:AbstractHttpMessageConverterReturnstrueif the given media type includes any of the supported media types.- Overrides:
- canWritein class- AbstractHttpMessageConverter<com.google.protobuf.Message>
- Parameters:
- mediaType- the media type to write, can be- nullif not specified. Typically the value of an- Acceptheader.
- Returns:
- trueif the supported media types are compatible with the media type, or if the media type is- null
 
- 
writeInternalprotected void writeInternal(com.google.protobuf.Message message, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException Description copied from class:AbstractHttpMessageConverterAbstract template method that writes the actual body. Invoked fromAbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).- Specified by:
- writeInternalin class- AbstractHttpMessageConverter<com.google.protobuf.Message>
- Parameters:
- message- the object to write to the output message
- outputMessage- the HTTP output message to write to
- Throws:
- IOException- in case of I/O errors
- HttpMessageNotWritableException- in case of conversion errors
 
- 
supportsRepeatableWritesprotected boolean supportsRepeatableWrites(com.google.protobuf.Message message) Description copied from class:AbstractHttpMessageConverterIndicates whether this message converter can write the given object multiple times.The default implementation returns false.- Overrides:
- supportsRepeatableWritesin class- AbstractHttpMessageConverter<com.google.protobuf.Message>
- Parameters:
- message- the object t
- Returns:
- trueif- tcan be written repeatedly;- falseotherwise
 
 
-