public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<com.google.protobuf.Message>
HttpMessageConverter that reads and writes Messages
using Google Protocol Buffers.
This converter supports by default "application/x-protobuf" and "text/plain"
with the official "com.google.protobuf:protobuf-java" library.
Other formats can be supported with additional libraries:
"application/json" with the official library
"com.google.protobuf:protobuf-java-util"
"application/json", "application/xml" and "text/html" (write only)
can be supported with the 3rd party library
"com.googlecode.protobuf-java-format:protobuf-java-format"
To generate Message Java classes, you need to install the protoc binary.
Requires Protobuf 2.6 and Protobuf Java Format 1.4, as of Spring 4.3. Supports up to Protobuf 3.0.0.
| Modifier and Type | Field and Description |
|---|---|
static Charset |
DEFAULT_CHARSET |
static MediaType |
PROTOBUF |
static String |
X_PROTOBUF_MESSAGE_HEADER |
static String |
X_PROTOBUF_SCHEMA_HEADER |
logger| Constructor and Description |
|---|
ProtobufHttpMessageConverter()
Construct a new instance.
|
ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer)
Construct a new instance with an
ExtensionRegistryInitializer
that allows the registration of message extensions. |
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
canWrite(MediaType mediaType)
This method overrides the parent implementation, since this HttpMessageConverter
can also produce
MediaType.HTML "text/html" ContentType. |
protected MediaType |
getDefaultContentType(com.google.protobuf.Message message)
Returns the default content type for the given type.
|
protected com.google.protobuf.Message |
readInternal(Class<? extends com.google.protobuf.Message> clazz,
HttpInputMessage inputMessage)
Abstract template method that reads the actual object.
|
protected boolean |
supports(Class<?> clazz)
Indicates whether the given class is supported by this converter.
|
protected void |
writeInternal(com.google.protobuf.Message message,
HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.
|
addDefaultHeaders, canRead, canRead, canWrite, getContentLength, getDefaultCharset, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, writepublic static final Charset DEFAULT_CHARSET
public static final MediaType PROTOBUF
public static final String X_PROTOBUF_SCHEMA_HEADER
public static final String X_PROTOBUF_MESSAGE_HEADER
public ProtobufHttpMessageConverter()
public ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer)
ExtensionRegistryInitializer
that allows the registration of message extensions.protected boolean supports(Class<?> clazz)
AbstractHttpMessageConvertersupports in class AbstractHttpMessageConverter<com.google.protobuf.Message>clazz - the class to test for supporttrue if supported; false otherwiseprotected MediaType getDefaultContentType(com.google.protobuf.Message message)
AbstractHttpMessageConverterAbstractHttpMessageConverter.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
supportedMediaTypes property, if any.
Can be overridden in subclasses.
getDefaultContentType in class AbstractHttpMessageConverter<com.google.protobuf.Message>message - the type to return the content type fornull if not knownprotected com.google.protobuf.Message readInternal(Class<? extends com.google.protobuf.Message> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
AbstractHttpMessageConverterAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage).readInternal in class AbstractHttpMessageConverter<com.google.protobuf.Message>clazz - the type of object to returninputMessage - the HTTP input message to read fromIOException - in case of I/O errorsHttpMessageNotReadableException - in case of conversion errorsprotected boolean canWrite(MediaType mediaType)
MediaType.HTML "text/html" ContentType.canWrite in class AbstractHttpMessageConverter<com.google.protobuf.Message>mediaType - the media type to write, can be null if not specified.
Typically the value of an Accept header.true if the supported media types are compatible with the media type,
or if the media type is nullprotected void writeInternal(com.google.protobuf.Message message,
HttpOutputMessage outputMessage)
throws IOException,
HttpMessageNotWritableException
AbstractHttpMessageConverterAbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).writeInternal in class AbstractHttpMessageConverter<com.google.protobuf.Message>message - the object to write to the output messageoutputMessage - the HTTP output message to write toIOException - in case of I/O errorsHttpMessageNotWritableException - in case of conversion errors