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 Summary
Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
-
Constructor Summary
ConstructorDescriptionObjectToStringHttpMessageConverter
(ConversionService conversionService) A constructor accepting aConversionService
to use to convert the (String) message body to/from the target class type.ObjectToStringHttpMessageConverter
(ConversionService conversionService, Charset defaultCharset) A constructor accepting aConversionService
as well as a default charset. -
Method Summary
Modifier and TypeMethodDescriptionboolean
This implementation checks if the given class is supported, and if the supported media types include the given media type.boolean
protected Long
getContentLength
(Object obj, @Nullable MediaType contentType) Returns the content length for the given type.protected Object
readInternal
(Class<?> clazz, HttpInputMessage inputMessage) Abstract template method that reads the actual object.void
setWriteAcceptCharset
(boolean writeAcceptCharset) protected boolean
Indicates whether the given class is supported by this converter.protected boolean
Indicates whether this message converter can write the given object multiple times.protected void
writeInternal
(Object obj, HttpOutputMessage outputMessage) Abstract template method that writes the actual body.Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
addDefaultHeaders, canRead, 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
-
Constructor Details
-
ObjectToStringHttpMessageConverter
A constructor accepting aConversionService
to use to convert the (String) message body to/from the target class type. This constructor usesStringHttpMessageConverter.DEFAULT_CHARSET
as the default charset.- Parameters:
conversionService
- the conversion service
-
ObjectToStringHttpMessageConverter
public ObjectToStringHttpMessageConverter(ConversionService conversionService, Charset defaultCharset) A constructor accepting aConversionService
as well as a default charset.- Parameters:
conversionService
- the conversion servicedefaultCharset
- the default charset
-
-
Method Details
-
setWriteAcceptCharset
public void setWriteAcceptCharset(boolean writeAcceptCharset) -
canRead
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 interfaceHttpMessageConverter<Object>
- Overrides:
canRead
in classAbstractHttpMessageConverter<Object>
- Parameters:
clazz
- the class to test for readabilitymediaType
- the media type to read (can benull
if not specified); typically the value of aContent-Type
header.- Returns:
true
if readable;false
otherwise
-
canWrite
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 interfaceHttpMessageConverter<Object>
- Overrides:
canWrite
in classAbstractHttpMessageConverter<Object>
- Parameters:
clazz
- the class to test for writabilitymediaType
- the media type to write (can benull
if not specified); typically the value of anAccept
header.- Returns:
true
if writable;false
otherwise
-
supports
Description copied from class:AbstractHttpMessageConverter
Indicates whether the given class is supported by this converter.- Specified by:
supports
in classAbstractHttpMessageConverter<Object>
- Parameters:
clazz
- the class to test for support- Returns:
true
if supported;false
otherwise
-
readInternal
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException Description copied from class:AbstractHttpMessageConverter
Abstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.- Specified by:
readInternal
in classAbstractHttpMessageConverter<Object>
- Parameters:
clazz
- the type of object to returninputMessage
- the HTTP input message to read from- Returns:
- the converted object
- Throws:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errors
-
writeInternal
Description copied from class:AbstractHttpMessageConverter
Abstract template method that writes the actual body. Invoked fromAbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.- Specified by:
writeInternal
in classAbstractHttpMessageConverter<Object>
- Parameters:
obj
- the object to write to the output messageoutputMessage
- the HTTP output message to write to- Throws:
IOException
- in case of I/O errors
-
getContentLength
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 classAbstractHttpMessageConverter<Object>
- Parameters:
obj
- the type to return the content length for- Returns:
- the content length, or
null
if not known
-
supportsRepeatableWrites
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 classAbstractHttpMessageConverter<Object>
- Parameters:
o
- the object t- Returns:
true
ift
can be written repeatedly;false
otherwise
-