Top Description Inners Fields Constructors Methods
org.springframework.messaging.converter

public Class ProtobufMessageConverter

extends AbstractMessageConverter
Class Inheritance
Known Direct Subclasses
org.springframework.messaging.converter.ProtobufJsonFormatMessageConverter
Imports
java.io.ByteArrayOutputStream, .IOException, .OutputStream, .OutputStreamWriter, java.lang.reflect.Method, java.nio.charset.Charset, .StandardCharsets, java.util.Map, com.google.protobuf.ExtensionRegistry, .Message, com.google.protobuf.util.JsonFormat, org.springframework.lang.Nullable, org.springframework.messaging.MessageHeaders, org.springframework.util.ClassUtils, .ConcurrentReferenceHashMap, .MimeType

An MessageConverter 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" with the official "com.google.protobuf:protobuf-java" library.

"application/json" can be supported with the official "com.google.protobuf:protobuf-java-util" 3.x, with 3.3 or higher recommended.

Authors
Parviz Rozikov, Rossen Stoyanchev
Since
5.2.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static interface
pack-priv static class
ProtobufMessageConverter.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 MimeType
PROTOBUF

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

private final ProtobufMessageConverter.ProtobufFormatSupport
private static final boolean
Inherited from org.springframework.messaging.converter.AbstractMessageConverter:
logger

Constructor Summary

AccessConstructor and Description
public
ProtobufMessageConverter()

Constructor with a default instance of ExtensionRegistry.

public
ProtobufMessageConverter(ExtensionRegistry extensionRegistry)

Constructor with a given ExtensionRegistry.

pack-priv

Method Summary

Modifier and TypeMethod and Description
protected boolean
protected Object
convertFromInternal(Message<?>
the input message
message
,
Class<?>
the target class for the conversion
targetClass
,
Object
an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}
conversionHint
)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.convertFromInternal.

Convert the message payload from serialized form to an Object.

protected Object
convertToInternal(Object
the Object to convert
payload
,
MessageHeaders
optional headers for the message (may be null)
headers
,
Object
an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}
conversionHint
)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.convertToInternal.

Convert the payload object to serialized form.

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

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

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

Implements abstract org.springframework.messaging.converter.AbstractMessageConverter.supports.

Whether the given class is supported by this converter.

Inherited from org.springframework.messaging.converter.AbstractMessageConverter:
addSupportedMimeTypescanConvertFromfromMessagefromMessagegetContentTypeResolvergetDefaultContentTypegetMimeTypegetResolvedTypegetSerializedPayloadClassgetSupportedMimeTypesisStrictContentTypeMatchsetContentTypeResolversetSerializedPayloadClasssetStrictContentTypeMatchsupportsMimeTypetoMessagetoMessage

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 MimeType PROTOBUF

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

protobufFormatSupportback to summary
private final ProtobufMessageConverter.ProtobufFormatSupport protobufFormatSupport
Annotations
@Nullable
protobufJsonFormatPresentback to summary
private static final boolean protobufJsonFormatPresent

Constructor Detail

ProtobufMessageConverterback to summary
public ProtobufMessageConverter()

Constructor with a default instance of ExtensionRegistry.

ProtobufMessageConverterback to summary
public ProtobufMessageConverter(ExtensionRegistry extensionRegistry)

Constructor with a given ExtensionRegistry.

ProtobufMessageConverterback to summary
pack-priv ProtobufMessageConverter(ProtobufMessageConverter.ProtobufFormatSupport formatSupport, ExtensionRegistry extensionRegistry)

Method Detail

canConvertToback to summary
protected boolean canConvertTo(Object payload, MessageHeaders headers)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.canConvertTo.

Annotations
@Override
convertFromInternalback to summary
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.convertFromInternal.

Doc from org.springframework.messaging.converter.AbstractMessageConverter.convertFromInternal.

Convert the message payload from serialized form to an Object.

Parameters
message:Message<?>

the input message

targetClass:Class<?>

the target class for the conversion

conversionHint:Object

an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}

Returns:Object

the result of the conversion, or null if the converter cannot perform the conversion

Annotations
@Override
convertToInternalback to summary
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.convertToInternal.

Doc from org.springframework.messaging.converter.AbstractMessageConverter.convertToInternal.

Convert the payload object to serialized form.

Parameters
payload:Object

the Object to convert

headers:MessageHeaders

optional headers for the message (may be null)

conversionHint:Object

an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}

Returns:Object

the resulting payload for the message, or null if the converter cannot perform the conversion

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

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

This method uses a ConcurrentReferenceHashMap for caching method lookups.

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

Implements abstract org.springframework.messaging.converter.AbstractMessageConverter.supports.

Doc from org.springframework.messaging.converter.AbstractMessageConverter.supports.

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
org.springframework.messaging.converter back to summary

pack-priv Interface ProtobufMessageConverter.ProtobufFormatSupport

Known Direct Implementers
org.springframework.messaging.converter.ProtobufMessageConverter.ProtobufJavaUtilSupport

Protobuf format support.

Method Summary

Modifier and TypeMethod and Description
public void
merge(Message<?> message, Charset charset, MimeType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder)

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

public MimeType[]
public boolean

Method Detail

mergeback to summary
public void merge(Message<?> message, Charset charset, MimeType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException, MessageConversionException
printback to summary
public void print(Message message, OutputStream output, MimeType contentType, Charset charset) throws IOException, MessageConversionException
supportedMediaTypesback to summary
public MimeType[] supportedMediaTypes()
supportsWriteOnlyback to summary
public boolean supportsWriteOnly(MimeType mediaType)
org.springframework.messaging.converter back to summary

pack-priv Class ProtobufMessageConverter.ProtobufJavaUtilSupport

extends Object
implements ProtobufFormatSupport
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.converter.ProtobufMessageConverter.ProtobufJavaUtilSupport
All Implemented Interfaces
org.springframework.messaging.converter.ProtobufMessageConverter.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 MimeType[]
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(Message<?> message, Charset charset, MimeType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException, MessageConversionException

Implements org.springframework.messaging.converter.ProtobufMessageConverter.ProtobufFormatSupport.merge.

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

Implements org.springframework.messaging.converter.ProtobufMessageConverter.ProtobufFormatSupport.print.

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

Implements org.springframework.messaging.converter.ProtobufMessageConverter.ProtobufFormatSupport.supportedMediaTypes.

Annotations
@Override
supportsWriteOnlyback to summary
public boolean supportsWriteOnly(MimeType mimeType)

Implements org.springframework.messaging.converter.ProtobufMessageConverter.ProtobufFormatSupport.supportsWriteOnly.

Annotations
@Override