org.springframework.http.converter.json

## Class GsonHttpMessageConverter

- java.lang.Object
- - org.springframework.http.converter.AbstractHttpMessageConverter<T>
  - - org.springframework.http.converter.AbstractGenericHttpMessageConverter<Object>
    - - org.springframework.http.converter.json.AbstractJsonHttpMessageConverter
      - - org.springframework.http.converter.json.GsonHttpMessageConverter

- All Implemented Interfaces:
    GenericHttpMessageConverter<Object>, HttpMessageConverter<Object>

      public class GsonHttpMessageConverter
      extends AbstractJsonHttpMessageConverter

  Implementation of `HttpMessageConverter` that can read and write JSON using the Google Gson library.

  This converter can be used to bind to typed beans or untyped `HashMap`s. By default, it supports `application/json` and `application/*+json` with `UTF-8` character set.

  Tested against Gson 2.8; compatible with Gson 2.0 and higher.

  Since:
    4.1
  Author:
    Roy Clarkson, Juergen Hoeller
  See Also:
    `Gson`, `GsonBuilder`, `setGson(com.google.gson.Gson)`

- - ### Field Summary

    - ### Fields inherited from class org.springframework.http.converter.json.AbstractJsonHttpMessageConverter

      `DEFAULT_CHARSET`

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

      `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `GsonHttpMessageConverter()` Construct a new `GsonHttpMessageConverter` with default configuration.
    `GsonHttpMessageConverter(com.google.gson.Gson gson)` Construct a new `GsonHttpMessageConverter` with the given delegate.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `com.google.gson.Gson` | `getGson()` Return the configured `Gson` instance for this converter.
    `protected Object` | `readInternal(Type resolvedType, Reader reader)` Template method that reads the JSON-bound object from the given `Reader`.
    `void` | `setGson(com.google.gson.Gson gson)` Set the `Gson` instance to use.
    `protected void` | `writeInternal(Object object, Type type, Writer writer)` Template method that writes the JSON-bound object to the given `Writer`.

    - ### Methods inherited from class org.springframework.http.converter.json.AbstractJsonHttpMessageConverter

      `read, readInternal, setJsonPrefix, setPrefixJson, writeInternal`

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

      `canRead, canWrite, supports, write, writeInternal`

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

      `addDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, 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

      `canRead, canWrite, getSupportedMediaTypes, getSupportedMediaTypes, read, write`

- - ### Constructor Detail

    - #### GsonHttpMessageConverter

          public GsonHttpMessageConverter()

      Construct a new `GsonHttpMessageConverter` with default configuration.

    - #### GsonHttpMessageConverter

          public GsonHttpMessageConverter(com.google.gson.Gson gson)

      Construct a new `GsonHttpMessageConverter` with the given delegate.
      Parameters:
        `gson` - the Gson instance to use
      Since:
        5.0

  - ### Method Detail

    - #### setGson

          public void setGson(com.google.gson.Gson gson)

      Set the `Gson` instance to use. If not set, a default `Gson` instance will be used.

      Setting a custom-configured `Gson` is one way to take further control of the JSON serialization process.

      See Also:
        `GsonHttpMessageConverter(Gson)`

    - #### getGson

          public com.google.gson.Gson getGson()

      Return the configured `Gson` instance for this converter.

    - #### readInternal

          protected Object readInternal(Type resolvedType,
                                        Reader reader)
                                 throws Exception

      Description copied from class: `AbstractJsonHttpMessageConverter`
      Template method that reads the JSON-bound object from the given `Reader`.
      Specified by:
        `readInternal` in class `AbstractJsonHttpMessageConverter`
      Parameters:
        `resolvedType` - the resolved generic type
        `reader` - the Reader to use
      Returns:
        the JSON-bound object
      Throws:
        `Exception` - in case of read/parse failures

    - #### writeInternal

          protected void writeInternal(Object object,
                                       @Nullable
                                       Type type,
                                       Writer writer)
                                throws Exception

      Description copied from class: `AbstractJsonHttpMessageConverter`
      Template method that writes the JSON-bound object to the given `Writer`.
      Specified by:
        `writeInternal` in class `AbstractJsonHttpMessageConverter`
      Parameters:
        `object` - the object to write to the output message
        `type` - the type of object to write (may be `null`)
        `writer` - the Writer to use
      Throws:
        `Exception` - in case of write failures