public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConverter<Object>
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>
logger
Constructor and Description |
---|
ObjectToStringHttpMessageConverter(ConversionService conversionService)
A constructor accepting a
ConversionService to use to convert the
(String) message body to/from the target class type. |
ObjectToStringHttpMessageConverter(ConversionService conversionService,
Charset defaultCharset)
A constructor accepting a
ConversionService as well as a default charset. |
Modifier and Type | Method and Description |
---|---|
boolean |
canRead(Class<?> clazz,
MediaType mediaType)
This implementation checks if the given class is supported,
and if the supported media types
include the given media type.
|
boolean |
canWrite(Class<?> clazz,
MediaType mediaType)
|
protected Long |
getContentLength(Object obj,
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)
Indicates whether the
Accept-Charset should be written to any outgoing request. |
protected boolean |
supports(Class<?> clazz)
Indicates whether the given class is supported by this converter.
|
protected void |
writeInternal(Object obj,
HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.
|
addDefaultHeaders, canRead, canWrite, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
public ObjectToStringHttpMessageConverter(ConversionService conversionService)
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.conversionService
- the conversion servicepublic ObjectToStringHttpMessageConverter(ConversionService conversionService, Charset defaultCharset)
ConversionService
as well as a default charset.conversionService
- the conversion servicedefaultCharset
- the default charsetpublic void setWriteAcceptCharset(boolean writeAcceptCharset)
Accept-Charset
should be written to any outgoing request.
Default is true
.
public boolean canRead(Class<?> clazz, MediaType mediaType)
AbstractHttpMessageConverter
canRead
in interface HttpMessageConverter<Object>
canRead
in class AbstractHttpMessageConverter<Object>
clazz
- the class to test for readabilitymediaType
- the media type to read, can be null
if not specified.
Typically the value of a Content-Type
header.true
if readable; false
otherwisepublic boolean canWrite(Class<?> clazz, MediaType mediaType)
AbstractHttpMessageConverter
canWrite
in interface HttpMessageConverter<Object>
canWrite
in class AbstractHttpMessageConverter<Object>
clazz
- the class to test for writabilitymediaType
- the media type to write, can be null
if not specified.
Typically the value of an Accept
header.true
if writable; false
otherwiseprotected boolean supports(Class<?> clazz)
AbstractHttpMessageConverter
supports
in class AbstractHttpMessageConverter<Object>
clazz
- the class to test for supporttrue
if supported; false
otherwiseprotected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException
AbstractHttpMessageConverter
AbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.readInternal
in class AbstractHttpMessageConverter<Object>
clazz
- the type of object to returninputMessage
- the HTTP input message to read fromIOException
- in case of I/O errorsprotected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException
AbstractHttpMessageConverter
AbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.writeInternal
in class AbstractHttpMessageConverter<Object>
obj
- the object to write to the output messageoutputMessage
- the HTTP output message to write toIOException
- in case of I/O errorsprotected Long getContentLength(Object obj, MediaType contentType)
AbstractHttpMessageConverter
By default, this returns null
, meaning that the content length is unknown.
Can be overridden in subclasses.
getContentLength
in class AbstractHttpMessageConverter<Object>
obj
- the type to return the content length fornull
if not known