Package org.springframework.http.converter

# Class StringHttpMessageConverter

java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<String>
org.springframework.http.converter.StringHttpMessageConverter

All Implemented Interfaces:
  `HttpMessageConverter<String>`

public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String>
Implementation of `HttpMessageConverter` that can read and write strings.

By default, this converter supports all media types (`*/*`), and writes with a `Content-Type` of `text/plain`. This can be overridden by setting the `supportedMediaTypes` property.

Since:
  3.0
Author:
  Arjen Poutsma, Juergen Hoeller

## Field Summary

Fields
Modifier and Type
Field
Description
`static final Charset`
`DEFAULT_CHARSET`
The default charset used by the converter.

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

`logger`

## Constructor Summary

Constructors
Constructor
Description
`StringHttpMessageConverter()`
A default constructor that uses `"ISO-8859-1"` as the default charset.
`StringHttpMessageConverter(Charset defaultCharset)`
A constructor accepting a default charset to use if the requested content type does not specify one.

## Method Summary

Modifier and Type
Method
Description
`protected void`
`addDefaultHeaders(HttpHeaders headers, String s, MediaType type)`
Add default headers to the output message.
`protected List<Charset>`
`getAcceptedCharsets()`
Return the list of supported `Charsets`.
`protected Long`
`getContentLength(String str, MediaType contentType)`
Returns the content length for the given type.
`protected String`
`readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage)`
Abstract template method that reads the actual object.
`void`
`setWriteAcceptCharset(boolean writeAcceptCharset)`
Whether the `Accept-Charset` header should be written to any outgoing request sourced from the value of `Charset.availableCharsets()`.
`boolean`
`supports(Class<?> clazz)`
Indicates whether the given class is supported by this converter.
`protected boolean`
`supportsRepeatableWrites(String s)`
Indicates whether this message converter can write the given object multiple times.
`protected void`
`writeInternal(String str, HttpOutputMessage outputMessage)`
Abstract template method that writes the actual body.

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

`canRead, canRead, canWrite, canWrite, 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

`getSupportedMediaTypes`

## Field Details

### DEFAULT_CHARSET

public static final Charset DEFAULT_CHARSET
The default charset used by the converter.

## Constructor Details

### StringHttpMessageConverter

public StringHttpMessageConverter()
A default constructor that uses `"ISO-8859-1"` as the default charset.

See Also:
  - `StringHttpMessageConverter(Charset)`

### StringHttpMessageConverter

public StringHttpMessageConverter(Charset defaultCharset)
A constructor accepting a default charset to use if the requested content type does not specify one.

## Method Details

### setWriteAcceptCharset

public void setWriteAcceptCharset(boolean writeAcceptCharset)
Whether the `Accept-Charset` header should be written to any outgoing request sourced from the value of `Charset.availableCharsets()`. The behavior is suppressed if the header has already been set.

As of 5.2, by default is set to `false`.

### supports

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

### readInternal

protected String readInternal(Class<? extends String> 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<String>`
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

### getContentLength

protected Long getContentLength(String str, @Nullable 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<String>`
Parameters:
  `str` - the type to return the content length for
Returns:
  the content length, or `null` if not known

### addDefaultHeaders

protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType type) throws IOException
Description copied from class: `AbstractHttpMessageConverter`
Add default headers to the output message.

This implementation delegates to `AbstractHttpMessageConverter.getDefaultContentType(Object)` if a content type was not provided, set if necessary the default character set, calls `AbstractHttpMessageConverter.getContentLength(T, org.springframework.http.MediaType)`, and sets the corresponding headers.

Overrides:
  `addDefaultHeaders` in class `AbstractHttpMessageConverter<String>`
Throws:
  `IOException`

### writeInternal

protected void writeInternal(String str, 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<String>`
Parameters:
  `str` - the object to write to the output message
  `outputMessage` - the HTTP output message to write to
Throws:
  `IOException` - in case of I/O errors

### getAcceptedCharsets

protected List<Charset> getAcceptedCharsets()
Return the list of supported `Charsets`.

By default, returns `Charset.availableCharsets()`. Can be overridden in subclasses.

Returns:
  the list of accepted charsets

### supportsRepeatableWrites

protected boolean supportsRepeatableWrites(String s)
Description copied from class: `AbstractHttpMessageConverter`
Indicates whether this message converter can write the given object multiple times.

The default implementation returns `false`.

Overrides:
  `supportsRepeatableWrites` in class `AbstractHttpMessageConverter<String>`
Parameters:
  `s` - the object t
Returns:
  `true` if `t` can be written repeatedly; `false` otherwise