Class ObjectToStringHttpMessageConverter
java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<Object>
org.springframework.http.converter.ObjectToStringHttpMessageConverter
- All Implemented Interfaces:
- HttpMessageConverter<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 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>
- Since:
- 3.2
- Author:
- Dmitry Katsubo, Rossen Stoyanchev
- 
Field SummaryFields inherited from class AbstractHttpMessageConverterlogger
- 
Constructor SummaryConstructorsConstructorDescriptionObjectToStringHttpMessageConverter(ConversionService conversionService) A constructor accepting aConversionServiceto use to convert the (String) message body to/from the target class type.ObjectToStringHttpMessageConverter(ConversionService conversionService, Charset defaultCharset) A constructor accepting aConversionServiceas well as a default charset.
- 
Method SummaryModifier and TypeMethodDescriptionbooleanThis implementation checks if the given class is supported, and if the supported media types include the given media type.booleanprotected LonggetContentLength(Object obj, @Nullable MediaType contentType) Returns the content length for the given type.protected ObjectreadInternal(Class<?> clazz, HttpInputMessage inputMessage) Abstract template method that reads the actual object.voidsetWriteAcceptCharset(boolean writeAcceptCharset) protected booleanIndicates whether the given class is supported by this converter.protected booleanIndicates whether this message converter can write the given object multiple times.protected voidwriteInternal(Object obj, HttpOutputMessage outputMessage) Abstract template method that writes the actual body.Methods inherited from class AbstractHttpMessageConverteraddDefaultHeaders, canRead, canWrite, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, writeMethods inherited from class Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface HttpMessageConvertergetSupportedMediaTypes
- 
Constructor Details- 
ObjectToStringHttpMessageConverterA constructor accepting aConversionServiceto use to convert the (String) message body to/from the target class type. This constructor usesStringHttpMessageConverter.DEFAULT_CHARSETas the default charset.- Parameters:
- conversionService- the conversion service
 
- 
ObjectToStringHttpMessageConverterpublic ObjectToStringHttpMessageConverter(ConversionService conversionService, Charset defaultCharset) A constructor accepting aConversionServiceas well as a default charset.- Parameters:
- conversionService- the conversion service
- defaultCharset- the default charset
 
 
- 
- 
Method Details- 
setWriteAcceptCharsetpublic void setWriteAcceptCharset(boolean writeAcceptCharset) 
- 
canReadDescription copied from class:AbstractHttpMessageConverterThis implementation checks if the given class is supported, and if the supported media types include the given media type.- Specified by:
- canReadin interface- HttpMessageConverter<Object>
- Overrides:
- canReadin class- AbstractHttpMessageConverter<Object>
- Parameters:
- clazz- the class to test for readability
- mediaType- the media type to read (can be- nullif not specified); typically the value of a- Content-Typeheader.
- Returns:
- trueif readable;- falseotherwise
 
- 
canWriteDescription copied from class:AbstractHttpMessageConverterThis implementation checks if the given class is supported, and if the supported media types include the given media type.- Specified by:
- canWritein interface- HttpMessageConverter<Object>
- Overrides:
- canWritein class- AbstractHttpMessageConverter<Object>
- Parameters:
- clazz- the class to test for writability
- mediaType- the media type to write (can be- nullif not specified); typically the value of an- Acceptheader.
- Returns:
- trueif writable;- falseotherwise
 
- 
supportsDescription copied from class:AbstractHttpMessageConverterIndicates whether the given class is supported by this converter.- Specified by:
- supportsin class- AbstractHttpMessageConverter<Object>
- Parameters:
- clazz- the class to test for support
- Returns:
- trueif supported;- falseotherwise
 
- 
readInternalprotected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException Description copied from class:AbstractHttpMessageConverterAbstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(Class, HttpInputMessage).- Specified by:
- readInternalin 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
- HttpMessageNotReadableException- in case of conversion errors
 
- 
writeInternalDescription copied from class:AbstractHttpMessageConverterAbstract template method that writes the actual body. Invoked fromAbstractHttpMessageConverter.write(T, MediaType, HttpOutputMessage).- Specified by:
- writeInternalin class- AbstractHttpMessageConverter<Object>
- Parameters:
- obj- the object to write to the output message
- outputMessage- the HTTP output message to write to
- Throws:
- IOException- in case of I/O errors
 
- 
getContentLengthDescription copied from class:AbstractHttpMessageConverterReturns 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:
- getContentLengthin class- AbstractHttpMessageConverter<Object>
- Parameters:
- obj- the type to return the content length for
- Returns:
- the content length, or nullif not known
 
- 
supportsRepeatableWritesDescription copied from class:AbstractHttpMessageConverterIndicates whether this message converter can write the given object multiple times.The default implementation returns false.- Overrides:
- supportsRepeatableWritesin class- AbstractHttpMessageConverter<Object>
- Parameters:
- o- the object t
- Returns:
- trueif- tcan be written repeatedly;- falseotherwise
 
 
-