org.springframework.http.converter

## Class ObjectToStringHttpMessageConverter

- java.lang.Object
- - org.springframework.http.converter.AbstractHttpMessageConverter<Object>
  - - org.springframework.http.converter.ObjectToStringHttpMessageConverter

All Implemented Interfaces:
  HttpMessageConverter<Object>

    public class ObjectToStringHttpMessageConverter
    extends AbstractHttpMessageConverter<Object>

An `HttpMessageConverter` that uses `StringHttpMessageConverter` for reading and writing content and a `ConversionService` for converting the String content to and from the target object type.

By default, this converter supports the media type `text/plain` only. This can be overridden by setting the `supportedMediaTypes` property. Example of usage:

     <bean class="org.springframework.http.converter.ObjectToStringHttpMessageConverter">
       <constructor-arg>
         <bean class="org.springframework.context.support.ConversionServiceFactoryBean"/>
       </constructor-arg>
     </bean>

Since:
  3.2
Author:
  Dmitry Katsubo, Rossen Stoyanchev

### Field Summary

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

`logger`

### Constructor Summary

Constructors
Constructor and Description
`ObjectToStringHttpMessageConverter(ConversionService conversionService)` A constructor accepting a `ConversionService` to use to convert the (String) message body to/from the target class type.
`ObjectToStringHttpMessageConverter(ConversionService conversionService, Charset defaultCharset)` A constructor accepting a `ConversionService` as well as a default charset.

### Method Summary

Methods
`boolean` `canRead(Class<?> clazz, MediaType mediaType)` This implementation checks if the given class is supported, and if the supported media types include the given media type.
`boolean` `canWrite(Class<?> clazz, MediaType mediaType)` This implementation checks if the given class is supported, and if the supported media types include the given media type.
`protected Long` `getContentLength(Object obj, MediaType contentType)` Returns the content length for the given type.
`protected Object` `readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage)` Abstract template method that reads the actual object.
`void` `setWriteAcceptCharset(boolean writeAcceptCharset)` Indicates whether the `Accept-Charset` should be written to any outgoing request.
`protected boolean` `supports(Class<?> clazz)` Indicates whether the given class is supported by this converter.
`protected void` `writeInternal(Object obj, HttpOutputMessage outputMessage)` Abstract template method that writes the actual body.

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

`canRead, canWrite, getDefaultContentType, getSupportedMediaTypes, read, setSupportedMediaTypes, write`

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

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

### Constructor Detail

#### ObjectToStringHttpMessageConverter

    public ObjectToStringHttpMessageConverter(ConversionService conversionService)

A constructor accepting a `ConversionService` to use to convert the (String) message body to/from the target class type. This constructor uses `StringHttpMessageConverter.DEFAULT_CHARSET` as the default charset.

Parameters:
  `conversionService` - the conversion service

#### ObjectToStringHttpMessageConverter

    public ObjectToStringHttpMessageConverter(ConversionService conversionService,
                                      Charset defaultCharset)

A constructor accepting a `ConversionService` as well as a default charset.

Parameters:
  `conversionService` - the conversion service
  `defaultCharset` - the default charset

### Method Detail

#### setWriteAcceptCharset

    public void setWriteAcceptCharset(boolean writeAcceptCharset)

Indicates whether the `Accept-Charset` should be written to any outgoing request.

Default is `true`.

#### canRead

    public boolean canRead(Class<?> clazz,
                  MediaType mediaType)

Description copied from class: `AbstractHttpMessageConverter`
This implementation checks if the given class is supported, and if the supported media types include the given media type.

Specified by:
  `canRead` in interface `HttpMessageConverter<Object>`
Overrides:
  `canRead` in class `AbstractHttpMessageConverter<Object>`
Parameters:
  `clazz` - the class to test for readability
  `mediaType` - the media type to read, can be `null` if not specified. Typically the value of a `Content-Type` header.
Returns:
  `true` if readable; `false` otherwise

#### canWrite

    public boolean canWrite(Class<?> clazz,
                   MediaType mediaType)

Description copied from class: `AbstractHttpMessageConverter`
This implementation checks if the given class is supported, and if the supported media types include the given media type.

Specified by:
  `canWrite` in interface `HttpMessageConverter<Object>`
Overrides:
  `canWrite` in class `AbstractHttpMessageConverter<Object>`
Parameters:
  `clazz` - the class to test for writability
  `mediaType` - the media type to write, can be `null` if not specified. Typically the value of an `Accept` header.
Returns:
  `true` if writable; `false` otherwise

#### 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<Object>`
Parameters:
  `clazz` - the class to test for support
Returns:
  `true` if supported; `false` otherwise

#### readInternal

    protected Object readInternal(Class<? extends Object> clazz,
                      HttpInputMessage inputMessage)
                           throws IOException

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<Object>`
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

#### writeInternal

    protected void writeInternal(Object obj,
                     HttpOutputMessage outputMessage)
                          throws IOException

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<Object>`
Parameters:
  `obj` - the object to write to the output message
  `outputMessage` - the message to write to
Throws:
  `IOException` - in case of I/O errors

#### getContentLength

    protected Long getContentLength(Object obj,
                        MediaType contentType)

Description copied from class: `AbstractHttpMessageConverter`
Returns the content length for the given type.

By default, this returns `null`, meaning that the content length is unknown. Can be overridden in subclasses.

Overrides:
  `getContentLength` in class `AbstractHttpMessageConverter<Object>`
Parameters:
  `obj` - the type to return the content length for
Returns:
  the content length, or `null` if not known