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>
public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<com.google.protobuf.Message>
AnHttpMessageConverterthat reads and writesMessages using Google Protocol Buffers.By default, it supports
"application/x-protobuf","text/plain","application/json","application/xml", while also writing"text/html".To generate
MessageJava classes, you need to install theprotocbinary.Requires Protobuf 2.6 and Protobuf Java Format 1.4, as of Spring 4.3.
- Since:
- 4.1
- Author:
- Alex Antonov, Brian Clozel, Juergen Hoeller
Field Summary
Fields Modifier and Type Field Description static CharsetDEFAULT_CHARSETstatic MediaTypePROTOBUFstatic StringX_PROTOBUF_MESSAGE_HEADERstatic StringX_PROTOBUF_SCHEMA_HEADERFields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
Constructor Summary
Constructors Constructor Description ProtobufHttpMessageConverter()Construct a new instance.ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer)Construct a new instance with anExtensionRegistryInitializerthat allows the registration of message extensions.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancanWrite(MediaType mediaType)This method overrides the parent implementation, since this HttpMessageConverter can also produceMediaType.HTML "text/html"ContentType.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 booleansupports(Class<?> clazz)Indicates whether the given class is supported by this converter.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.AbstractHttpMessageConverter
addDefaultHeaders, canRead, canRead, canWrite, getContentLength, getDefaultCharset, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
Field Detail
DEFAULT_CHARSET
public static final Charset DEFAULT_CHARSET
X_PROTOBUF_SCHEMA_HEADER
public static final String X_PROTOBUF_SCHEMA_HEADER
- See Also:
- Constant Field Values
X_PROTOBUF_MESSAGE_HEADER
public static final String X_PROTOBUF_MESSAGE_HEADER
- See Also:
- Constant Field Values
Constructor Detail
ProtobufHttpMessageConverter
public ProtobufHttpMessageConverter()
Construct a new instance.
ProtobufHttpMessageConverter
public ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer)
Construct a new instance with anExtensionRegistryInitializerthat allows the registration of message extensions.
Method Detail
supports
protected boolean supports(Class<?> clazz)
Description copied from class:AbstractHttpMessageConverterIndicates whether the given class is supported by this converter.- Specified by:
supportsin classAbstractHttpMessageConverter<com.google.protobuf.Message>- Parameters:
clazz- the class to test for support- Returns:
trueif supported;falseotherwise
getDefaultContentType
protected MediaType getDefaultContentType(com.google.protobuf.Message message)
Description 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 classAbstractHttpMessageConverter<com.google.protobuf.Message>- Parameters:
message- the type to return the content type for- Returns:
- the content type, or
nullif not known
readInternal
protected 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 classAbstractHttpMessageConverter<com.google.protobuf.Message>- Parameters:
clazz- the type of object to returninputMessage- the HTTP input message to read from- Returns:
- the converted object
- Throws:
IOException- in case of I/O errorsHttpMessageNotReadableException- in case of conversion errors
canWrite
protected boolean canWrite(MediaType mediaType)
This method overrides the parent implementation, since this HttpMessageConverter can also produceMediaType.HTML "text/html"ContentType.- Overrides:
canWritein classAbstractHttpMessageConverter<com.google.protobuf.Message>- Parameters:
mediaType- the media type to write, can benullif not specified. Typically the value of anAcceptheader.- Returns:
trueif the supported media types are compatible with the media type, or if the media type isnull
writeInternal
protected 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 classAbstractHttpMessageConverter<com.google.protobuf.Message>- Parameters:
message- the object to write to the output messageoutputMessage- the HTTP output message to write to- Throws:
IOException- in case of I/O errorsHttpMessageNotWritableException- in case of conversion errors