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

public Class ObjectToStringHttpMessageConverter

extends AbstractHttpMessageConverter<Object>
Class Inheritance
Imports
java.io.IOException, java.nio.charset.Charset, org.springframework.core.convert.ConversionService, org.springframework.http.HttpInputMessage, .HttpOutputMessage, .MediaType, org.springframework.lang.Nullable, org.springframework.util.Assert

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 through the supportedMediaTypes property.

A usage example:

<bean class="org.springframework.http.converter.ObjectToStringHttpMessageConverter">
  <constructor-arg>
    <bean class="org.springframework.context.support.ConversionServiceFactoryBean"/>
  </constructor-arg>
</bean>
Authors
Dmitry Katsubo, Rossen Stoyanchev
Since
3.2

Field Summary

Modifier and TypeField and Description
private final ConversionService
private final StringHttpMessageConverter
Inherited from org.springframework.http.converter.AbstractHttpMessageConverter:
logger

Constructor Summary

AccessConstructor and Description
public
ObjectToStringHttpMessageConverter(ConversionService
the conversion service
conversionService
)

A constructor accepting a ConversionService to use to convert the (String) message body to/from the target class type.

public
ObjectToStringHttpMessageConverter(ConversionService
the conversion service
conversionService
,
Charset
the default charset
defaultCharset
)

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

Method Summary

Modifier and TypeMethod and Description
public boolean
canRead(Class<?>
the class to test for readability
clazz
,
MediaType
the media type to read (can be null if not specified); typically the value of a Content-Type header.
mediaType
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canRead.

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

This implementation checks if the given class is supported, and if the supported media types include the given media type.
public boolean
canWrite(Class<?>
the class to test for writability
clazz
,
MediaType
the media type to write (can be null if not specified); typically the value of an Accept header.
mediaType
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canWrite.

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

This implementation checks if the given class is supported, and if the supported media types include the given media type.
protected Long
getContentLength(Object
the type to return the content length for
obj
,
MediaType contentType)

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

Returns the content length for the given type.
protected Object
readInternal(Class<?>
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
protected 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(Object
the object t
o
)

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

Indicates whether this message converter can write the given object multiple times.
protected void
writeInternal(Object
the object to write to the output message
obj
,
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:
addDefaultHeaderscanReadcanWritegetDefaultCharsetgetDefaultContentTypegetSupportedMediaTypesreadsetDefaultCharsetsetSupportedMediaTypeswrite

Field Detail

conversionServiceback to summary
private final ConversionService conversionService
stringHttpMessageConverterback to summary
private final StringHttpMessageConverter stringHttpMessageConverter

Constructor Detail

ObjectToStringHttpMessageConverterback to summary
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:ConversionService

the conversion service

ObjectToStringHttpMessageConverterback to summary
public ObjectToStringHttpMessageConverter(ConversionService conversionService, Charset defaultCharset)

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

Parameters
conversionService:ConversionService

the conversion service

defaultCharset:Charset

the default charset

Method Detail

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

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canRead.

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

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

This implementation checks if the given class is supported, and if the supported media types include the given media type.

Parameters
clazz:Class<?>

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

the class to test for readability

mediaType:MediaType

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

the media type to read (can be null if not specified); typically the value of a Content-Type header.

Returns:boolean

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

true if readable; false otherwise

Annotations
@Override
canWriteback to summary
public boolean canWrite(Class<?> clazz, MediaType mediaType)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canWrite.

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

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

This implementation checks if the given class is supported, and if the supported media types include the given media type.

Parameters
clazz:Class<?>

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

the class to test for writability

mediaType:MediaType

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

the media type to write (can be null if not specified); typically the value of an Accept header.

Returns:boolean

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

true if writable; false otherwise

Annotations
@Override
getContentLengthback to summary
protected Long getContentLength(Object obj, 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
obj:Object

the type to return the content length for

Returns:Long

the content length, or null if not known

Annotations
@Override
readInternalback to summary
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException

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<?>

the type of object to return

inputMessage:HttpInputMessage

the HTTP input message to read from

Returns:Object

the converted object

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
HttpMessageNotReadableException:
in case of conversion errors
setWriteAcceptCharsetback to summary
public void setWriteAcceptCharset(boolean writeAcceptCharset)

Delegates to StringHttpMessageConverter#setWriteAcceptCharset(boolean).

supportsback to summary
protected 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(Object o)

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
o:Object

the object t

Returns:boolean

true if t can be written repeatedly; false otherwise

Annotations
@Override
writeInternalback to summary
protected void writeInternal(Object obj, 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
obj:Object

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