Top Description Fields Constructors Methods
org.springframework.http.converter

public Class StringHttpMessageConverter

extends AbstractHttpMessageConverter<String>
Class Inheritance
Imports
java.io.IOException, java.nio.charset.Charset, .StandardCharsets, java.util.ArrayList, .List, org.springframework.http.HttpHeaders, .HttpInputMessage, .HttpOutputMessage, .MediaType, org.springframework.lang.Nullable, org.springframework.util.Assert, .StreamUtils

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.

Authors
Arjen Poutsma, Juergen Hoeller
Since
3.0

Field Summary

Modifier and TypeField and Description
private static final MediaType
private volatile List<Charset>
public static final Charset
DEFAULT_CHARSET

The default charset used by the converter.

private boolean
Inherited from org.springframework.http.converter.AbstractHttpMessageConverter:
logger

Constructor Summary

AccessConstructor and Description
public
StringHttpMessageConverter()

A default constructor that uses "ISO-8859-1" as the default charset.

public
StringHttpMessageConverter(Charset defaultCharset)

A constructor accepting a default charset to use if the requested content type does not specify one.

Method Summary

Modifier and TypeMethod and Description
protected void
protected List<Charset>

Returns:

the list of accepted charsets
getAcceptedCharsets
()

Return the list of supported Charsets.

protected Long
getContentLength(String
the type to return the content length for
str
,
MediaType contentType)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getContentLength.

Returns the content length for the given type.
private Charset
protected String
readInternal(Class<? extends String>
the type of object to return
clazz
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Abstract template method that reads the actual object.
public void
setWriteAcceptCharset(boolean writeAcceptCharset)

Whether the Accept-Charset header should be written to any outgoing request sourced from the value of Charset#availableCharsets().

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

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates whether the given class is supported by this converter.
protected boolean
supportsRepeatableWrites(String
the object t
s
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Indicates whether this message converter can write the given object multiple times.
protected void
writeInternal(String
the object to write to the output message
str
,
HttpOutputMessage
the HTTP output message to write to
outputMessage
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Abstract template method that writes the actual body.
Inherited from org.springframework.http.converter.AbstractHttpMessageConverter:
canReadcanReadcanWritecanWritegetDefaultCharsetgetDefaultContentTypegetSupportedMediaTypesreadsetDefaultCharsetsetSupportedMediaTypeswrite

Field Detail

APPLICATION_PLUS_JSONback to summary
private static final MediaType APPLICATION_PLUS_JSON
availableCharsetsback to summary
private volatile List<Charset> availableCharsets
Annotations
@Nullable
DEFAULT_CHARSETback to summary
public static final Charset DEFAULT_CHARSET

The default charset used by the converter.

writeAcceptCharsetback to summary
private boolean writeAcceptCharset

Constructor Detail

StringHttpMessageConverterback to summary
public StringHttpMessageConverter()

A default constructor that uses "ISO-8859-1" as the default charset.

See Also
StringHttpMessageConverter(Charset)
StringHttpMessageConverterback to summary
public StringHttpMessageConverter(Charset defaultCharset)

A constructor accepting a default charset to use if the requested content type does not specify one.

Method Detail

addDefaultHeadersback to summary
protected void addDefaultHeaders(HttpHeaders headers, String s, MediaType type) throws IOException

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.addDefaultHeaders.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.addDefaultHeaders.

Add default headers to the output message.

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

Annotations
@Override
getAcceptedCharsetsback to summary
protected List<Charset> getAcceptedCharsets()

Return the list of supported Charsets.

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

Returns:List<Charset>

the list of accepted charsets

getContentLengthback to summary
protected Long getContentLength(String str, MediaType contentType)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getContentLength.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.getContentLength.

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.

Parameters
str:String

the type to return the content length for

Returns:Long

the content length, or null if not known

Annotations
@Override
getContentTypeCharsetback to summary
private Charset getContentTypeCharset(MediaType contentType)
readInternalback to summary
protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Abstract template method that reads the actual object. Invoked from read.

Parameters
clazz:Class<? extends String>

the type of object to return

inputMessage:HttpInputMessage

the HTTP input message to read from

Returns:String

the converted object

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
setWriteAcceptCharsetback to summary
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.

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

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates 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
supportsRepeatableWritesback to summary
protected boolean supportsRepeatableWrites(String s)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Indicates whether this message converter can write the given object multiple times.

Default implementation returns false.

Parameters
s:String

the object t

Returns:boolean

true if t can be written repeatedly; false otherwise

Annotations
@Override
writeInternalback to summary
protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Abstract template method that writes the actual body. Invoked from write.

Parameters
str:String

the object to write to the output message

outputMessage:HttpOutputMessage

the HTTP output message to write to

Annotations
@Override
Exceptions
IOException:
in case of I/O errors