org.springframework.http.converter.protobuf

## 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. 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`.

  Since:
    4.1
  Author:
    Alex Antonov, Brian Clozel, Juergen Hoeller, Sebastien Deleuze
  See Also:
    `FormatFactory`, `JsonFormat`, `ProtobufJsonFormatHttpMessageConverter`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static Charset` | `DEFAULT_CHARSET` The default charset used by the converter.
    `static MediaType` | `PROTOBUF` The media-type for protobuf `application/x-protobuf`.
    `static String` | `X_PROTOBUF_MESSAGE_HEADER` The HTTP header containing the protobuf message.
    `static String` | `X_PROTOBUF_SCHEMA_HEADER` The HTTP header containing the protobuf schema.

    - ### Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter

      `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ProtobufHttpMessageConverter()` Construct a new `ProtobufHttpMessageConverter`.
    `ProtobufHttpMessageConverter(com.google.protobuf.ExtensionRegistry extensionRegistry)` Construct a new `ProtobufHttpMessageConverter` with a registry that specifies protocol message extensions.
    `ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer)` Deprecated. as of Spring Framework 5.1, use `ProtobufHttpMessageConverter(ExtensionRegistry)` instead

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    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(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.

    - ### Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter

      `addDefaultHeaders, canRead, canRead, canWrite, getContentLength, getDefaultCharset, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.springframework.http.converter.HttpMessageConverter

      `getSupportedMediaTypes`

- - ### Field Detail

    - #### DEFAULT_CHARSET

          public static final Charset DEFAULT_CHARSET

      The default charset used by the converter.

    - #### PROTOBUF

          public static final MediaType PROTOBUF

      The media-type for protobuf `application/x-protobuf`.

    - #### X_PROTOBUF_SCHEMA_HEADER

          public static final String X_PROTOBUF_SCHEMA_HEADER

      The HTTP header containing the protobuf schema.
      See Also:
        Constant Field Values

    - #### X_PROTOBUF_MESSAGE_HEADER

          public static final String X_PROTOBUF_MESSAGE_HEADER

      The HTTP header containing the protobuf message.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### ProtobufHttpMessageConverter

          public ProtobufHttpMessageConverter()

      Construct a new `ProtobufHttpMessageConverter`.

    - #### ProtobufHttpMessageConverter

          @Deprecated
          public ProtobufHttpMessageConverter(@Nullable
                                                          ExtensionRegistryInitializer registryInitializer)

      Deprecated. as of Spring Framework 5.1, use `ProtobufHttpMessageConverter(ExtensionRegistry)` instead
      Construct a new `ProtobufHttpMessageConverter` with an initializer that allows the registration of message extensions.
      Parameters:
        `registryInitializer` - an initializer for message extensions

    - #### ProtobufHttpMessageConverter

          public ProtobufHttpMessageConverter(com.google.protobuf.ExtensionRegistry extensionRegistry)

      Construct a new `ProtobufHttpMessageConverter` with a registry that specifies protocol message extensions.
      Parameters:
        `extensionRegistry` - the registry to populate

  - ### Method Detail

    - #### supports

          protected boolean supports(Class<?> clazz)

      Description copied from class: `AbstractHttpMessageConverter`
      Indicates whether the given class is supported by this converter.
      Specified by:
        `supports` in class `AbstractHttpMessageConverter<com.google.protobuf.Message>`
      Parameters:
        `clazz` - the class to test for support
      Returns:
        `true` if supported; `false` otherwise

    - #### getDefaultContentType

          protected MediaType getDefaultContentType(com.google.protobuf.Message message)

      Description copied from class: `AbstractHttpMessageConverter`
      Returns the default content type for the given type. Called when `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.

      Overrides:
        `getDefaultContentType` in class `AbstractHttpMessageConverter<com.google.protobuf.Message>`
      Parameters:
        `message` - the type to return the content type for
      Returns:
        the content type, or `null` if 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: `AbstractHttpMessageConverter`
      Abstract template method that reads the actual object. Invoked from `AbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)`.
      Specified by:
        `readInternal` in 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

    - #### canWrite

          protected boolean canWrite(@Nullable
                                     MediaType mediaType)

      Description copied from class: `AbstractHttpMessageConverter`
      Returns `true` if the given media type includes any of the supported media types.
      Overrides:
        `canWrite` in class `AbstractHttpMessageConverter<com.google.protobuf.Message>`
      Parameters:
        `mediaType` - the media type to write, can be `null` if not specified. Typically the value of an `Accept` header.
      Returns:
        `true` if the supported media types are compatible with the media type, or if the media type is `null`

    - #### writeInternal

          protected void writeInternal(com.google.protobuf.Message message,
                                       HttpOutputMessage outputMessage)
                                throws IOException,
                                       HttpMessageNotWritableException

      Description copied from class: `AbstractHttpMessageConverter`
      Abstract template method that writes the actual body. Invoked from `AbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)`.
      Specified by:
        `writeInternal` in 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