@ -6,29 +6,34 @@
@@ -6,29 +6,34 @@
<title > Introduction</title>
<para > The goal of Spring's REST support is to make the development of
'RESTful' Web services and applications easier. Client-side access to
RESTful resources is greatly simplified using Spring
<classname > RestTemplate</classname> . <classname > RestTemplate</classname>
follows in the footsteps of other 'Template' classes in Spring such as
<classname > JdbcTemplate</classname> and
RESTful Web services and applications easier. </para>
<para > Client-side access to RESTful resources is greatly simplified using
Spring <classname > RestTemplate</classname> .
<classname > RestTemplate</classname> follows in the footsteps of other
template classes in Spring such as <classname > JdbcTemplate</classname> and
<classname > JmsTemplate</classname> . Instead of dealing with a verbose
lower level API such as Apache Commons HttpClient to create RESTful
request, RestTemplate provides one liner methods that are purpose built
for RESTful programming. On the server-side, Spring's REST support is
based upon Spring's existing annotation based MVC framework. (For those
interested in the rational for that decision, and say not implenting
JAX-RS, read Arjen Putsma's SpringSource TeamBlog <ulink
lower level API such as Apache Commons <classname > HttpClient</classname>
to create RESTful request, RestTemplate provides one liner methods that
are purpose built for RESTful programming. </para>
<para > On the server-side, Spring's REST support is based upon Spring's
existing annotation based MVC framework. (For those interested in the
rational for that decision, and for not implementing JAX-RS, read Arjen
Poutsma's SpringSource TeamBlog <ulink
url="http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/">entry</ulink> .)
With little effort, you can marshall data out of a RESTful request using
@RequestMapping and @PathVariable annotations and return different views
as determined by the request's Context-Type header.</para>
<classname > @RequestMapping</classname> and
<classname > @PathVariable</classname> annotations and return different
views as determined by the request's <literal > Context-Type</literal>
header.</para>
<para > In this chapter we describe all the features of Spring's REST
support. It is divided into two main two chapters, one for the server-side
and one for the client-side. For those new to Spring's <link
linkend="mvc">MVC framework</link> , you may want to read through the
reference documentation on <link linkend= "mvc-annotation" > annotation-based
controller configuration</link> to undestand the general programming
controller configuration</link> to under stand the general programming
model.</para>
</section>
@ -41,13 +46,13 @@
@@ -41,13 +46,13 @@
linkend="mvc-servlet">DispatcherServlet</link> .</para>
<section id= "rest-uritemplate" >
<title > URI t emplates</title>
<title > URI T emplates</title>
<para > RESTful services use URIs to name resourses. To facili ate
<para > RESTful services use URIs to name resources. To facilit ate
accessing the information contained in a URI, its structure follows
conventions so that it can easily be described in a parameterized form.
The <ulink url= "http://bitworking.org/projects/URI-Templates/" > proposed
RFC</ulink> for URI Templates defines how a URI is parameterized. For
RFC</ulink> for URI Templates defines how an URI is parameterized. For
example, the URI Template</para>
<programlisting > http://www.example.com/users/{userid}</programlisting>
@ -57,11 +62,11 @@
@@ -57,11 +62,11 @@
<programlisting > http://www.example.com/users/fred</programlisting>
<para > When processing an request the URI can be compared to an expected
<para > When processing a request the URI can be compared to an expected
URI Template in order to extract a collection of variables.</para>
<para > Spring uses the <classname > @RequestMapping</classname> annotation
to define the URI Template for the request.
<para > Spring uses the <classname > @RequestMapping</classname> method
annotation to define the URI Template for the request.
The<classname > @PathVariable</classname> annotation is used to extract
the value of the template variables and assign their value to a method
variable. A Spring controller method to process above example is shown
@ -78,9 +83,9 @@ public String getUser(@PathVariable String userId) {
@@ -78,9 +83,9 @@ public String getUser(@PathVariable String userId) {
<section id= "rest-path-variable" >
<title > Mapping RESTful URLs with the @PathVariable annotation</title>
<para > The <classname > @PathVariable</classname> method level annotation
is used to indicate that a method parameter should be bound to the
value of a URI template variable.</para>
<para > The <classname > @PathVariable</classname> method parameter
annotation is used to indicate that a method parameter should be bound
to the value of a URI template variable.</para>
<para > The following code snippet shows the use of a single
<classname > @PathVariable</classname> in a controller method:</para>
@ -95,9 +100,9 @@ public String findOwner(<emphasis role="bold">@PathVariable</emphasis> String ow
@@ -95,9 +100,9 @@ public String findOwner(<emphasis role="bold">@PathVariable</emphasis> String ow
<para > The URI Template "<literal > /owners/{ownerId}</literal> "
specifies the variable name ownerId. When the controller handles this
request, the value of ownerId is set the the value in the request URI.
For example, when a request comes in for /owners/fred, the value
'fred' is bound to the method parameter <literal > String
request, the value of ownerId is set the value in the request URI. For
example, when a request comes in for /owners/fred, the value 'fred' is
bound to the method parameter <literal > String
ownerId</literal> .</para>
<para > The matching of method parameter names to URI Template variable
@ -113,8 +118,8 @@ public String findOwner(<emphasis role="bold">@PathVariable</emphasis>("ownerId"
@@ -113,8 +118,8 @@ public String findOwner(<emphasis role="bold">@PathVariable</emphasis>("ownerId"
</programlisting>
<para > The name of the method parameter does not matter in this case,
so you may also use create a controlled method with the signature
shown below</para>
so you may also use a controller method with the signature shown
below</para>
<programlisting language= "java" > @RequestMapping("/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(<emphasis role= "bold" > @PathVariable</emphasis> ("ownerId") String theOwner, Model model) {
@ -157,6 +162,93 @@ public class RelativePathUriTemplateController {
@@ -157,6 +162,93 @@ public class RelativePathUriTemplateController {
not correct.</para>
</tip>
</section>
<section >
<title > Mapping the request body with the @RequestBody
annotation</title>
<para > The <classname > @RequestBody</classname> method parameter
annotation is used to indicate that a method parameter should be bound
to the value of the HTTP request body. For example, </para>
<programlisting language= "java" > @RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}</programlisting>
<para > The conversion of the request body to the method argument is
done using a <interfacename > HttpMessageConverter</interfacename> .
<interfacename > HttpMessageConverter</interfacename> is responsible for
converting for converting from the HTTP request message to an object
and converting from an object to the HTTP response body.
<classname > DispatcherServlet</classname> supports annotation based
processing using the
<classname > DefaultAnnotationHandlerMapping</classname> and
<classname > AnnotationMethodHandlerAdapter</classname> . In Spring 3 the
<classname > AnnotationMethodHandlerAdapter</classname> has been
extended to support the <classname > @RequestBody</classname> and has
several <interfacename > HttpMessageConverters</interfacename>
registered by default, these are</para>
<itemizedlist >
<listitem >
<para > <classname > ByteArrayHttpMessageConverter</classname> -
converts byte arrays</para>
</listitem>
<listitem >
<para > <classname > StringHttpMessageConverter</classname> - converts
strings</para>
</listitem>
<listitem >
<para > <classname > FormHttpMessageConverter</classname> - converts
form data to/from a MultiValueMap< String, String> </para>
</listitem>
<listitem >
<para > <classname > SourceHttpMessageConverter</classname> - convert
to/from a javax.xml.transform.Source;</para>
</listitem>
<listitem >
<para > <classname > MarshallingHttpMessageConverter</classname> -
convert to/from an object using the
<classname > org.springframework.oxm</classname> package.</para>
</listitem>
</itemizedlist>
<para > More information on these converters can be found in the section
<link linkend= "rest-message-conversion" > Message Converters</link> .
</para>
<para > The <classname > MarshallingHttpMessageConverter</classname>
requires a <interfacename > Marshaller</interfacename> and
<interfacename > Unmarshaller</interfacename> from the
<classname > org.springframework.oxm</classname> package to be
configured on an instance of
<classname > AnnotationMethodHandlerAdapter</classname> in the
application context. For example</para>
<programlisting language= "xml" > < bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
< property name="messageConverters">
< util:list id="beanList">
< ref bean="stringHttpMessageConverter"/>
< ref bean="marshallingHttpMessageConverter"/>
< /util:list>
< /property
< /bean>
< bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
< bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
< property name="marshaller" ref="castorMarshaller" />
< property name="unmarshaller" ref="castorMarshaller" />
< /bean>
< bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>
</programlisting>
</section>
</section>
<section id= "rest-multiple-representations" >
@ -205,7 +297,7 @@ public class RelativePathUriTemplateController {
@@ -205,7 +297,7 @@ public class RelativePathUriTemplateController {
view based on the file extension or <literal > Accept</literal> header of
the HTTP request. <classname > ContentNegotiatingViewResolver</classname>
does not perform the view resolution itself, but instead delegates to a
list of view resolvers set using the property
list of view resolvers set using the bean property
<literal > ViewResolvers</literal> .</para>
<para > The <classname > ContentNegotiatingViewResolver</classname> selects
@ -221,7 +313,7 @@ public class RelativePathUriTemplateController {
@@ -221,7 +313,7 @@ public class RelativePathUriTemplateController {
Context-Type was 'text/xml' is a compatible match.</para>
<para > To support the resolution of a view based on a file extension,
<classname > ContentNegotiatingViewResolver</classname> 's property
<classname > ContentNegotiatingViewResolver</classname> 's bean property
<literal > MediaTypes</literal> is used to specify a mapping of file
extensions to media types. For more information on the algorithm to
determine the request media type, refer to the API documentation for
@ -309,7 +401,7 @@ public class ContentController {
@@ -309,7 +401,7 @@ public class ContentController {
<section id= "rest-views" >
<title > Views</title>
<para > Several views were added to Spring 3 to help support creating
<para > Several views were added in Spring 3 to help support creating
RESTful services. They are:</para>
<itemizedlist >
@ -325,29 +417,34 @@ public class ContentController {
@@ -325,29 +417,34 @@ public class ContentController {
<listitem >
<para > <classname > MarshallingView</classname> - returns an XML
representation using Spring's Objecct/ XML mapping (OXM)
representation using Spring's Object to XML mapping (OXM)
functionality</para>
</listitem>
</itemizedlist>
<para > Available separately is the <classname > JacksonJsonView</classname>
included as part of the Spring JavaScript project.</para>
<note >
<para > Available separately is the
<classname > JacksonJsonView</classname> included as part of the Spring
JavaScript project.</para>
</note>
<section id= "rest-feedview" >
<title > Feed Views</title>
<para > Both <classname > AbstractAtomFeedView</classname> and
<classname > AbstractRssFeedView</classname> inherit from the base class
<classname > AbstractFeedView< T> </classname> and are used to
provide Atom and RSS Feed views respectfully. They are based on
java.net's <ulink url= "https://rome.dev.java.net" > ROME</ulink> project
and located in the package org.springframework.web.servlet.view.feed.
The <classname > AbstractAtomFeedView</classname> requires you to
<classname > AbstractFeedView</classname> and are used to provide Atom
and RSS Feed views respectfully. They are based on java.net's <ulink
url="https://rome.dev.java.net">ROME</ulink> project and are located
in the package
<literal > org.springframework.web.servlet.view.feed</literal> . </para>
<para > <classname > AbstractAtomFeedView</classname> requires you to
implement the <methodname > buildFeedEntries</methodname> method and
optionally override the <methodname > buildFeedMetadata</methodname>
method (the default implementation is empty), as shown below</para>
<programlisting > public class SampleContentAtomView extends AbstractAtomFeedView {
<programlisting language= "java" > public class SampleContentAtomView extends AbstractAtomFeedView {
@Override
protected void buildFeedMetadata(Map< String, Object> model, Feed feed, HttpServletRequest request) {
@ -365,7 +462,7 @@ public class ContentController {
@@ -365,7 +462,7 @@ public class ContentController {
<para > Similar requirements apply for implementing
<classname > AbstractRssFeedView</classname> , as shown below</para>
<programlisting > public class SampleContentAtomView extends AbstractRssFeedView {
<programlisting language= "java" > public class SampleContentAtomView extends AbstractRssFeedView {
@Override
protected void buildFeedMetadata(Map< String, Object> model, Channel feed, HttpServletRequest request) {
@ -387,15 +484,22 @@ public class ContentController {
@@ -387,15 +484,22 @@ public class ContentController {
automatically be written to the response object after the method
returns. </para>
<para > For an example implementation of creating a Atom view please
refer to Alef Arendsen's SpringSource TeamBlog <ulink
<para > For an example of creating a Atom view please refer to Alef
Arendsen's SpringSource TeamBlog <ulink
url="http://blog.springsource.com/2009/03/16/adding-an-atom-view-to-an-application-using-springs-rest-support/">entry</ulink> .</para>
</section>
<section >
<title > XML Marshalling View</title>
<para > The MarhsallingView</para>
<para > The <classname > MarhsallingView</classname> uses a XML
<interfacename > Marshaller</interfacename> defined in the
<classname > org.springframework.oxm</classname> package to render the
response content as XML. The object to be marshalled can be set
explicitly using <classname > MarhsallingView</classname> 's
<property > modelKey</property> bean property. Alternatively, the view
will iterate over all model properties marhsall only those types that
are supported by the <interfacename > Marshaller</interfacename> .</para>
</section>
</section>
@ -471,7 +575,7 @@ public String deletePet(@PathVariable int ownerId, @PathVariable int petId) {
@@ -471,7 +575,7 @@ public String deletePet(@PathVariable int ownerId, @PathVariable int petId) {
renders the view again, and compares the two hashes. If they are equal,
a <literal > 304</literal> is returned. It is important to note that this
filter will not save processing power, as the view is still rendered.
The only thing it saves is bandwith, as the rendered response is not
The only thing it saves is bandwid th, as the rendered response is not
sent back over the wire.</para>
<para > Deep ETags are a bit more complicated. In this case, the ETag is
@ -486,62 +590,354 @@ public String deletePet(@PathVariable int ownerId, @PathVariable int petId) {
@@ -486,62 +590,354 @@ public String deletePet(@PathVariable int ownerId, @PathVariable int petId) {
<section id= "rest-exception" >
<title > Exception Handling</title>
<para > @ExceptionHandler</para>
<para > The <classname > @ExceptionHandling</classname> method annotation is
used within a controller to specify which method will be invoked when an
exception of a specific type is thrown during the execution of
controller methods. For example</para>
<programlisting language= "java" > @Controller
public class SimpleController {
// other controller method omitted
@ExceptionHandler(IOException.class)
public String handleIOException(IOException ex, HttpServletRequest request) {
return ClassUtils.getShortName(ex.getClass());
}
}</programlisting>
<para > will invoke the 'handlerIOException' method when a
<classname > java.io.IOException</classname> is thrown.</para>
<para > The <classname > @ExceptionHandler</classname> value can be set to
an array of Exception types. If an exception is thrown matches one of
the types in the list, then the method annotated with the matching
<classname > @ExceptionHandler</classname> will be invoked. If the
annotation value is not set then the exception types listed as method
arguments are used. </para>
<para > Much like standard controller methods annotated with a
<classname > @RequestMapping</classname> annotation, the method arguments
and return values of <classname > @ExceptionHandler</classname> methods
are very flexible. For example, the
<classname > HttpServletRequest</classname> can be access in Servlet
environments and the <classname > PortletRequest</classname> in Portlet
environments. The return type can be a <classname > String</classname> ,
which is interpreted as a view name or a
<classname > ModelAndView</classname> object. Please refer to the API
documentation for more details. </para>
</section>
</section>
<section id= "rest-client-access" >
<title > Accessing RESTful services on the Client</title>
<para > Spring provides a client-side API blah blah</para>
<para > The <classname > RestTemplate</classname> is the core class for
client-side access to RESTful services. It is conceptually similar to
other template classes in Spring, such as
<classname > JdbcTemplate</classname> and <classname > JmsTemplate</classname>
and other template classes found in other Spring portfolio projects.
<classname > RestTemplate</classname> 's behavior is customized by providing
callback methods and configuring the
<interfacename > HttpMessageConverter</interfacename> used to marshal
objects into the HTTP request body and to unmarshall any response back
into an object. As it is common to use XML as a message format, Spring
provides a <classname > MarshallingHttpMessageConverter</classname> that
uses the Object-to-XML framework that is part of the
<classname > org.springframework.oxm</classname> package. This gives you a
wide range of choices of XML to Object mapping technologies to choose
from. </para>
<para > This section describes how to use the
<classname > RestTemplate</classname> and its associated
<interfacename > HttpMessageConverters</interfacename> .</para>
<section id= "rest-resttemplate" >
<title > RestTemplate</title>
<para > blah blah</para>
<para > Invoking RESTful services in Java is typically done using a helper
class such as Jakarta Commons <classname > HttpClient</classname> . For
common REST operations this approach is too low level as shown
below.</para>
<programlisting > String uri = "http://example.com/hotels/1/bookings";
PostMethod post = new PostMethod(uri);
String request = // create booking request content
post.setRequestEntity(new StringRequestEntity(request));
httpClient.executeMethod(post);
if (HttpStatus.SC_CREATED == post.getStatusCode()) {
Header location = post.getRequestHeader("Location");
if (location != null) {
System.out.println("Created new booking at :" + location.getValue());
}
}</programlisting>
<para > RestTemplate provides higher level methods that correspond to
each of the six main HTTP methods that make invoking many RESTful
services a one-liner and enforce REST best practices.</para>
<table >
<title > Overview of RestTemplate methods</title>
<tgroup cols= "2" >
<tbody >
<row >
<entry > <emphasis role= "bold" > HTTP Method</emphasis> </entry>
<entry > <emphasis role= "bold" > RestTemplate
Method</emphasis> </entry>
</row>
<row >
<entry > DELETE</entry>
<entry > <ulink
url="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html#delete(String,%20String...)">delete(String
url, String… urlVariables)</ulink> </entry>
</row>
<row >
<entry > GET</entry>
<entry > <ulink
url="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html#getForObject(String,%20Class,%20String...)">getForObject(String
url, Class< T> responseType, String…
urlVariables)</ulink> </entry>
</row>
<row >
<entry > HEAD</entry>
<entry > <ulink
url="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html#headForHeaders(String,%20String...)">headForHeaders(String
url, String… urlVariables)</ulink> </entry>
</row>
<row >
<entry > OPTIONS</entry>
<entry > <ulink
url="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html#optionsForAllow(String,%20String...)">optionsForAllow(String
url, String… urlVariables)</ulink> </entry>
</row>
<row >
<entry > POST</entry>
<entry > <ulink
url="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html#postForLocation(String,%20Object,%20String...)">postForLocation(String
url, Object request, String… urlVariables)</ulink> </entry>
</row>
<row >
<entry > PUT</entry>
<entry > <ulink
url="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html#put(String,%20Object,%20String...)">put(String
url, Object request, String…urlVariables)</ulink> </entry>
</row>
</tbody>
</tgroup>
</table>
<para > The names of <classname > RestTemplate</classname> methods follow a
naming convention, the first part indicates what HTTP method is being
invoked and the second part indicates what is returned. For example, the
method <methodname > getForObject</methodname> will perform a GET, convert
the HTTP response into an object type of your choice, and returns that
object. The method <methodname > postForLocation</methodname> will do a
POST, converting the given object into a HTTP request, and returns the
response HTTP Location header where the newly created object can be
found In case of an exception processing the HTTP request, an exception
of the type <classname > RestClientException</classname> will be
thrown.</para>
<para > Objects passed to and returned from these methods are converted to
and from HTTP messages by
<interfacename > HttpMessageConverter</interfacename> instances.
Converters for the main mime types are registered by default, but you
can also write your own converter and register it via the
<methodname > messageConverters</methodname> bean property. The default
converter instances registered with the template are
<classname > ByteArrayHttpMessageConverter</classname> ,
<classname > StringHttpMessageConverter</classname> ,
<classname > FormHttpMessageConverter</classname> and
<classname > SourceHttpMessageConverter</classname> . You can override
these defaults using the <methodname > messageConverters</methodname> bean
property as would be required if using the
<classname > MarshallingHttpMessageConverter</classname> .</para>
<para > Each method takes URI template arguments in two forms, either as a
<literal > String</literal> variable length argument or a
<literal > Map< String,String> </literal> . For example, </para>
<programlisting language= "java" > String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}",
String.class,"42", "21");
</programlisting>
<para > using variable length arguments and </para>
<programlisting language= "java" > Map< String, String> vars = Collections.singletonMap("hotel", 42);
String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/rooms/{hotel}", String.class, vars);
</programlisting>
<para > using a <literal > Map< String,String> </literal> . </para>
<para > To create an instance of <classname > RestTemplate</classname> you
can simply call the default constructor. This will use standard Java
classes from the <literal > java.net</literal> package as the underlying
implementation to create HTTP requests. This can be overridden by
specifying an implementation of
<interfacename > ClientHttpRequestFactory</interfacename> . Spring provides
the implementation
<classname > CommonsClientHttpRequestFactory</classname> that uses the
Jakarta Commons <classname > HttpClient</classname> to create requests.
<classname > CommonsClientHttpRequestFactory</classname> is configured
using an instance of
<classname > org.apache.commons.httpclient.HttpClient</classname> which
can in turn be configured with credentials information or connection
pooling functionality.</para>
<para > The previous example using Jakarta Commons
<classname > HttpClient</classname> directly rewritten to use the
<classname > RestTemplate</classname> is shown below </para>
<programlisting > uri = "http://example.com/hotels/{id}/bookings";
RestTemplate template = new RestTemplate();
Booking booking = // create booking object
URI location = template.postForLocation(uri, booking, String.class, "1");
</programlisting>
<para > The general callback interface is
<interfacename > RequestCallback</interfacename> and is called when the
execute method is invoked.</para>
<programlisting language= "java" > public < T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor< T> responseExtractor,
String... urlVariables)
// also has an overload with urlVariables as a Map< String, String> .</programlisting>
<para > The <interfacename > RequestCallback</interfacename> interface is
defined as </para>
<programlisting language= "java" > public interface RequestCallback {
void doWithRequest(ClientHttpRequest request) throws IOException;
}</programlisting>
<para > and allows you to manipulate the request headers and write to the
request body. When using the execute method you do not have to worry
about any resource management, the template will always close the
request and handle any errors. Refer to the API documentation for more
information on using the execute method and the meaning of its other
method arguments.</para>
</section>
<section id= "rest-message-conversion" >
<title > HTTP Message Conversion</title>
<para > blah blah</para>
<para > Objects passed to and returned from the methods getForObject(),
postForLocation(), and put() and are converted to HTTP requests and from
HTTP responses by <interfacename > HttpMessageConverters</interfacename> .
The <interfacename > HttpMessageConverter</interfacename> interface is
show below to give you a better feel for its functionality</para>
<programlisting language= "java" > public interface HttpMessageConverter< T> {
// Indicate whether the given class is supported by this converter.
boolean supports(Class< ? extends T> clazz);
// Return the list of MediaType objects supported by this converter.
List< MediaType> getSupportedMediaTypes();
// Read an object of the given type form the given input message, and returns it.
T read(Class< T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;
// Write an given object to the given output message.
void write(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;
}</programlisting>
<para > Concrete implementations for the main media (mime) types are
provided in the framework and are registered by default with the
<classname > RestTemplate</classname> on the client-side and with
<classname > AnnotationMethodHandlerAdapter</classname> on the
server-side. </para>
<para > The implementations of
<classname > HttpMessageConverter</classname> s are described in the
following sections. For all converters a default media type is used but
can be overridden by setting the
<classname > supportedMediaTypes</classname> bean property</para>
<section id= "rest-string-converter" >
<title > StringHttpMessageConverter</title>
<para > </para>
<para > </para>
<para > A <interfacename > HttpMessageConverter</interfacename>
implementation that can read and write Strings from the HTTP request
and response. By default, this converter supports all text media types
(<literal > text/*</literal> ), and writes with a
<literal > Content-Type</literal> of
<literal > text/plain</literal> .</para>
</section>
<section id= "rest-form-converter" >
<title > FormHttpMessageConverter</title>
<para > </para>
<para > </para>
<para > A <interfacename > HttpMessageConverter</interfacename>
implementation that can read and write form data from the HTTP request
and response. By default, this converter reads and writes the media
type (<literal > application/x-www-form-urlencoded</literal> ). Form data
is read from and written into a <literal > MultiValueMap< String,
String> </literal> . </para>
</section>
<section id= "rest-byte-converter" >
<title > ByteArrayMessageConverter</title>
<para > </para>
<para > </para>
<para > A <interfacename > HttpMessageConverter</interfacename>
implementation that can read and write byte arrays from the HTTP
request and response. By default, this converter supports all media
types (<literal > */*</literal> ), and writes with a
<literal > Content-Type</literal> of
<literal > application/octet-stream</literal> . This can be overridden by
setting the <property > supportedMediaTypes</property> property, and
overriding <literal > getContentType(byte[])</literal> . </para>
</section>
<section id= "rest-marhsalling-converter" >
<title > MarshallingHttpMessageConverter</title>
<para > </para>
<para > </para>
<para > A <interfacename > HttpMessageConverter</interfacename>
implementation that can read and write XML using Spring's
<interfacename > Marshaller</interfacename> and
<interfacename > Unmarshaller</interfacename> abstractions from the
<classname > org.springframework.oxm</classname> package. This converter
requires a <interfacename > Marshaller</interfacename> and
<interfacename > Unmarshaller</interfacename> before it can be used.
These can be injected via constructor or bean properties. By default
this converter supports (<literal > text/xml</literal> ) and
(<literal > application/xml</literal> ).</para>
</section>
<section id= "rest-source-converter" >
<title > SourceHttpMessageConverter</title>
<para > </para>
<para > A <interfacename > HttpMessageConverter</interfacename>
implementation that can read and write
<classname > javax.xml.transform.Source</classname> from the HTTP
request and response. Only <classname > DOMSource</classname> ,
<classname > SAXSource</classname> , and
<classname > StreamSource</classname> are supported. By default, this
converter supports (<literal > text/xml</literal> ) and
(<literal > application/xml</literal> ).</para>
</section>
</section>
</section>