public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<Message>
HttpMessageConverter
that reads and writes com.google.protobuf.Message
s
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. Other formats can be
supported with one of the following additional libraries on the classpath:
"application/json"
, "application/xml"
, and "text/html"
(write-only)
with the "com.googlecode.protobuf-java-format:protobuf-java-format"
third-party library
"application/json"
with the official "com.google.protobuf:protobuf-java-util"
for Protobuf 3 (see ProtobufJsonFormatHttpMessageConverter
for a configurable variant)
Requires Protobuf 2.6 or higher (and Protobuf Java Format 1.4 or higher for formatting).
This converter will auto-adapt to Protobuf 3 and its default protobuf-java-util
JSON
format if the Protobuf 2 based protobuf-java-format
isn't present; however, for more
explicit JSON setup on Protobuf 3, consider ProtobufJsonFormatHttpMessageConverter
.
FormatFactory
,
JsonFormat
,
ProtobufJsonFormatHttpMessageConverter
Modifier and Type | Class and Description |
---|---|
(package private) static interface |
ProtobufHttpMessageConverter.ProtobufFormatSupport |
(package private) static class |
ProtobufHttpMessageConverter.ProtobufJavaFormatSupport |
(package private) static class |
ProtobufHttpMessageConverter.ProtobufJavaUtilSupport |
Modifier and Type | Field and Description |
---|---|
static java.nio.charset.Charset |
DEFAULT_CHARSET |
private ExtensionRegistry |
extensionRegistry |
private static java.util.Map<java.lang.Class<?>,java.lang.reflect.Method> |
methodCache |
static MediaType |
PROTOBUF |
private ProtobufHttpMessageConverter.ProtobufFormatSupport |
protobufFormatSupport |
static java.lang.String |
X_PROTOBUF_MESSAGE_HEADER |
static java.lang.String |
X_PROTOBUF_SCHEMA_HEADER |
logger
Constructor and Description |
---|
ProtobufHttpMessageConverter()
Construct a new
ProtobufHttpMessageConverter . |
ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer)
Construct a new
ProtobufHttpMessageConverter with an
initializer that allows the registration of message extensions. |
ProtobufHttpMessageConverter(ProtobufHttpMessageConverter.ProtobufFormatSupport formatSupport,
ExtensionRegistryInitializer registryInitializer) |
Modifier and Type | Method and Description |
---|---|
protected boolean |
canWrite(MediaType mediaType)
Returns
true if the given media type includes any of the
supported media types. |
protected MediaType |
getDefaultContentType(Message message)
Returns the default content type for the given type.
|
private Message.Builder |
getMessageBuilder(java.lang.Class<? extends Message> clazz)
Create a new
Message.Builder instance for the given class. |
protected Message |
readInternal(java.lang.Class<? extends Message> clazz,
HttpInputMessage inputMessage)
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"
|
protected boolean |
supports(java.lang.Class<?> clazz)
Indicates whether the given class is supported by this converter.
|
protected void |
writeInternal(Message message,
HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.
|
addDefaultHeaders, canRead, canRead, canWrite, getContentLength, getDefaultCharset, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
public static final java.nio.charset.Charset DEFAULT_CHARSET
public static final MediaType PROTOBUF
public static final java.lang.String X_PROTOBUF_SCHEMA_HEADER
public static final java.lang.String X_PROTOBUF_MESSAGE_HEADER
private static final java.util.Map<java.lang.Class<?>,java.lang.reflect.Method> methodCache
private final ExtensionRegistry extensionRegistry
@Nullable private final ProtobufHttpMessageConverter.ProtobufFormatSupport protobufFormatSupport
public ProtobufHttpMessageConverter()
ProtobufHttpMessageConverter
.public ProtobufHttpMessageConverter(@Nullable ExtensionRegistryInitializer registryInitializer)
ProtobufHttpMessageConverter
with an
initializer that allows the registration of message extensions.registryInitializer
- an initializer for message extensionsProtobufHttpMessageConverter(@Nullable ProtobufHttpMessageConverter.ProtobufFormatSupport formatSupport, @Nullable ExtensionRegistryInitializer registryInitializer)
protected boolean supports(java.lang.Class<?> clazz)
AbstractHttpMessageConverter
supports
in class AbstractHttpMessageConverter<Message>
clazz
- the class to test for supporttrue
if supported; false
otherwiseprotected MediaType getDefaultContentType(Message message)
AbstractHttpMessageConverter
AbstractHttpMessageConverter.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<Message>
message
- the type to return the content type fornull
if not knownprotected Message readInternal(java.lang.Class<? extends Message> clazz, HttpInputMessage inputMessage) throws java.io.IOException, HttpMessageNotReadableException
AbstractHttpMessageConverter
AbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.readInternal
in class AbstractHttpMessageConverter<Message>
clazz
- the type of object to returninputMessage
- the HTTP input message to read fromjava.io.IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errorsprivate Message.Builder getMessageBuilder(java.lang.Class<? extends Message> clazz) throws java.lang.Exception
Message.Builder
instance for the given class.
This method uses a ConcurrentReferenceHashMap for caching method lookups.
java.lang.Exception
protected boolean canWrite(@Nullable MediaType mediaType)
AbstractHttpMessageConverter
true
if the given media type includes any of the
supported media types.canWrite
in class AbstractHttpMessageConverter<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 null
protected void writeInternal(Message message, HttpOutputMessage outputMessage) throws java.io.IOException, HttpMessageNotWritableException
AbstractHttpMessageConverter
AbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.writeInternal
in class AbstractHttpMessageConverter<Message>
message
- the object to write to the output messageoutputMessage
- the HTTP output message to write tojava.io.IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errorsprivate void setProtoHeader(HttpOutputMessage response, Message message)
Note: outputMessage.getBody()
should not have been called
before because it writes HTTP headers (making them read only).