public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConverter<java.lang.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,
java.nio.charset.Charset defaultCharset)
A constructor accepting a
ConversionService as well as a default charset. |
Modifier and Type | Method and Description |
---|---|
boolean |
canRead(java.lang.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(java.lang.Class<?> clazz,
MediaType mediaType)
|
protected java.lang.Long |
getContentLength(java.lang.Object obj,
MediaType contentType)
Returns the content length for the given type.
|
protected java.lang.Object |
readInternal(java.lang.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(java.lang.Class<?> clazz)
Indicates whether the given class is supported by this converter.
|
protected void |
writeInternal(java.lang.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, java.nio.charset.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(java.lang.Class<?> clazz, @Nullable MediaType mediaType)
AbstractHttpMessageConverter
canRead
in interface HttpMessageConverter<java.lang.Object>
canRead
in class AbstractHttpMessageConverter<java.lang.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(java.lang.Class<?> clazz, @Nullable MediaType mediaType)
AbstractHttpMessageConverter
canWrite
in interface HttpMessageConverter<java.lang.Object>
canWrite
in class AbstractHttpMessageConverter<java.lang.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(java.lang.Class<?> clazz)
AbstractHttpMessageConverter
supports
in class AbstractHttpMessageConverter<java.lang.Object>
clazz
- the class to test for supporttrue
if supported; false
otherwiseprotected java.lang.Object readInternal(java.lang.Class<?> clazz, HttpInputMessage inputMessage) throws java.io.IOException
AbstractHttpMessageConverter
AbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.readInternal
in class AbstractHttpMessageConverter<java.lang.Object>
clazz
- the type of object to returninputMessage
- the HTTP input message to read fromjava.io.IOException
- in case of I/O errorsprotected void writeInternal(java.lang.Object obj, HttpOutputMessage outputMessage) throws java.io.IOException
AbstractHttpMessageConverter
AbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.writeInternal
in class AbstractHttpMessageConverter<java.lang.Object>
obj
- the object to write to the output messageoutputMessage
- the HTTP output message to write tojava.io.IOException
- in case of I/O errorsprotected java.lang.Long getContentLength(java.lang.Object obj, @Nullable MediaType contentType)
AbstractHttpMessageConverter
By default, this returns null
, meaning that the content length is unknown.
Can be overridden in subclasses.
getContentLength
in class AbstractHttpMessageConverter<java.lang.Object>
obj
- the type to return the content length fornull
if not known