Spring for Android

org.springframework.http.converter
Class ObjectToStringHttpMessageConverter

java.lang.Object
  extended by org.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
      extended by org.springframework.http.converter.ObjectToStringHttpMessageConverter
All Implemented Interfaces:
HttpMessageConverter<java.lang.Object>

public class ObjectToStringHttpMessageConverter
extends AbstractHttpMessageConverter<java.lang.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 by setting the supportedMediaTypes property.

Since:
2.0
Author:
Dmitry Katsubo, Rossen Stoyanchev

Constructor Summary
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.
 
Method Summary
 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)
          This implementation checks if the given class is supported, and if the supported media types include the given media type.
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<? extends java.lang.Object> 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.
 
Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
canRead, canWrite, getDefaultContentType, getSupportedMediaTypes, read, setSupportedMediaTypes, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectToStringHttpMessageConverter

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 - the conversion service

ObjectToStringHttpMessageConverter

public ObjectToStringHttpMessageConverter(ConversionService conversionService,
                                          java.nio.charset.Charset defaultCharset)
A constructor accepting a ConversionService as well as a default charset.

Parameters:
conversionService - the conversion service
defaultCharset - the default charset
Method Detail

setWriteAcceptCharset

public void setWriteAcceptCharset(boolean writeAcceptCharset)
Indicates whether the Accept-Charset should be written to any outgoing request.

Default is true.


canRead

public boolean canRead(java.lang.Class<?> clazz,
                       MediaType mediaType)
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 interface HttpMessageConverter<java.lang.Object>
Overrides:
canRead in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the class to test for readability
mediaType - the media type to read, can be null if not specified. Typically the value of a Content-Type header.
Returns:
true if readable; false otherwise

canWrite

public boolean canWrite(java.lang.Class<?> clazz,
                        MediaType mediaType)
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 interface HttpMessageConverter<java.lang.Object>
Overrides:
canWrite in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the class to test for writability
mediaType - the media type to write, can be null if not specified. Typically the value of an Accept header.
Returns:
true if writable; false otherwise

supports

protected boolean supports(java.lang.Class<?> clazz)
Description copied from class: AbstractHttpMessageConverter
Indicates whether the given class is supported by this converter.

Specified by:
supports in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the class to test for support
Returns:
true if supported; false otherwise

readInternal

protected java.lang.Object readInternal(java.lang.Class<? extends java.lang.Object> clazz,
                                        HttpInputMessage inputMessage)
                                 throws java.io.IOException
Description copied from class: AbstractHttpMessageConverter
Abstract template method that reads the actual object. Invoked from AbstractHttpMessageConverter.read(java.lang.Class, org.springframework.http.HttpInputMessage).

Specified by:
readInternal in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
clazz - the type of object to return
inputMessage - the HTTP input message to read from
Returns:
the converted object
Throws:
java.io.IOException - in case of I/O errors

writeInternal

protected void writeInternal(java.lang.Object obj,
                             HttpOutputMessage outputMessage)
                      throws java.io.IOException
Description copied from class: AbstractHttpMessageConverter
Abstract template method that writes the actual body. Invoked from AbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).

Specified by:
writeInternal in class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
obj - the object to write to the output message
outputMessage - the HTTP output message to write to
Throws:
java.io.IOException - in case of I/O errors

getContentLength

protected java.lang.Long getContentLength(java.lang.Object obj,
                                          MediaType contentType)
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 class AbstractHttpMessageConverter<java.lang.Object>
Parameters:
obj - the type to return the content length for
Returns:
the content length, or null if not known

Spring for Android