@ -946,9 +946,9 @@ plugging in third-party or custom solutions here.
@@ -946,9 +946,9 @@ plugging in third-party or custom solutions here.
[[rest-client-access]]
=== Accessing REST endpoints
=== REST Endpoints
The Spring Framework has two choices for client-side access to REST endpoints:
The Spring Framework provides two choices for making calls to REST endpoints:
* <<rest-resttemplate>> -- the original Spring REST client with an API similar to other
template classes in Spring such as `JdbcTemplate`, `JmsTemplate` and others.
@ -963,130 +963,75 @@ to support high concurrency more efficiently (i.e. using a small number of threa
@@ -963,130 +963,75 @@ to support high concurrency more efficiently (i.e. using a small number of threa
[[rest-resttemplate]]
==== RestTemplate
The `RestTemplate` provides a higher level API over HTTP client libraries with 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.
The `RestTemplate` provides a higher level API over HTTP client libraries. It makes it
easy to invoke REST endpoints in a single line. It exposes the following groups of
URI location = template.postForLocation("http://example.com/people", person);
The "Content-Type" header of the request does not need to be set explicitly. In most cases
a compatible message converter can be found based on the source Object type, and the chosen
message converter will set the content type accordingly. If necessary, you can use the
`exchange` methods to provide the "Content-Type" request header explicitly, and that in
turn will influence what message converter is selected.
On a GET, the body of the response is deserialized to an output Object:
Person person = restTemplate.getForObject("http://example.com/people/{id}", Person.class, 42);
The "Accept" header of the request does not need to be set explicitly. In most cases
a compatible message converter can be found based on the expected response type, which
then helps to populate the "Accept" header. If necessary, you can use the `exchange`
methods to provide the "Accept" header explicitly.
By default `RestTemplate` registers all built-in
<<rest-message-conversion,message converters>>, depending on classpath checks that help
to determine what optional conversion libraries are present. You can also set the message
converters to use explicitly.
[[rest-message-conversion]]
==== HTTP Message Converters
===== Message Conversion
[.small]#<<web-reactive.adoc#webflux-codecs,Same in Spring WebFlux>>#
The `spring-web` module contains the `HttpMessageConverter` contract for reading and
@ -1222,52 +1168,46 @@ The implementations of ``HttpMessageConverter``s are described in the following
@@ -1222,52 +1168,46 @@ The implementations of ``HttpMessageConverter``s are described in the following
For all converters a default media type is used but can be overridden by setting the
`supportedMediaTypes` bean property
[[rest-string-converter]]
===== StringHttpMessageConverter
[[rest-message-converters-tbl]]
.HttpMessageConverter Implementations
[cols="1,3"]
|===
| MessageConverter | Description
An `HttpMessageConverter` implementation that can read and write Strings from the HTTP
| `StringHttpMessageConverter`
| An `HttpMessageConverter` implementation that can read and write Strings from the HTTP
request and response. By default, this converter supports all text media types (
`text/{asterisk}`), and writes with a `Content-Type` of `text/plain`.
[[rest-form-converter]]
===== FormHttpMessageConverter
An `HttpMessageConverter` implementation that can read and write form data from the HTTP
| `FormHttpMessageConverter`
| An `HttpMessageConverter` implementation that can read and write form data from the HTTP
request and response. By default, this converter reads and writes the media type
`application/x-www-form-urlencoded`. Form data is read from and written into a
`MultiValueMap<String, String>`.
[[rest-byte-converter]]
===== ByteArrayHttpMessageConverter
An `HttpMessageConverter` implementation that can read and write byte arrays from the
| `ByteArrayHttpMessageConverter`
| An `HttpMessageConverter` implementation that can read and write byte arrays from the
HTTP request and response. By default, this converter supports all media types ( `{asterisk}/{asterisk}`),
and writes with a `Content-Type` of `application/octet-stream`. This can be overridden
by setting the `supportedMediaTypes` property, and overriding `getContentType(byte[])`.
[[rest-marhsalling-converter]]
===== MarshallingHttpMessageConverter
An `HttpMessageConverter` implementation that can read and write XML using Spring's
| `MarshallingHttpMessageConverter`
| An `HttpMessageConverter` implementation that can read and write XML using Spring's
`Marshaller` and `Unmarshaller` abstractions from the `org.springframework.oxm` package.
This converter requires a `Marshaller` and `Unmarshaller` before it can be used. These
can be injected via constructor or bean properties. By default this converter supports (
`text/xml`) and ( `application/xml`).
[[rest-mapping-json-converter]]
===== MappingJackson2HttpMessageConverter
An `HttpMessageConverter` implementation that can read and write JSON using Jackson's
| `MappingJackson2HttpMessageConverter`
| An `HttpMessageConverter` implementation that can read and write JSON using Jackson's
`ObjectMapper`. JSON mapping can be customized as needed through the use of Jackson's
provided annotations. When further control is needed, a custom `ObjectMapper` can be
injected through the `ObjectMapper` property for cases where custom JSON
serializers/deserializers need to be provided for specific types. By default this
converter supports ( `application/json`).
[[rest-mapping-xml-converter]]
===== MappingJackson2XmlHttpMessageConverter
An `HttpMessageConverter` implementation that can read and write XML using
| `MappingJackson2XmlHttpMessageConverter`
| An `HttpMessageConverter` implementation that can read and write XML using
`XmlMapper`. XML mapping can be customized as needed through the use of JAXB
or Jackson's provided annotations. When further control is needed, a custom `XmlMapper`
@ -1275,27 +1215,45 @@ can be injected through the `ObjectMapper` property for cases where custom XML
@@ -1275,27 +1215,45 @@ can be injected through the `ObjectMapper` property for cases where custom XML
serializers/deserializers need to be provided for specific types. By default this
converter supports ( `application/xml`).
[[rest-source-converter]]
===== SourceHttpMessageConverter
An `HttpMessageConverter` implementation that can read and write
| `SourceHttpMessageConverter`
| An `HttpMessageConverter` implementation that can read and write
`javax.xml.transform.Source` from the HTTP request and response. Only `DOMSource`,
`SAXSource`, and `StreamSource` are supported. By default, this converter supports (
`text/xml`) and ( `application/xml`).
[[rest-buffered-image-converter]]
===== BufferedImageHttpMessageConverter
An `HttpMessageConverter` implementation that can read and write
| `BufferedImageHttpMessageConverter`
| An `HttpMessageConverter` implementation that can read and write
`java.awt.image.BufferedImage` from the HTTP request and response. This converter reads
and writes the media type supported by the Java I/O API.
|===
[[rest-template-jsonview]]
===== Jackson JSON Views
It is possible to specify a http://wiki.fasterxml.com/JacksonJsonViews[Jackson JSON View]
to serialize only a subset of the object properties. For example:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
MappingJacksonValue value = new MappingJacksonValue(new User("eric", "7!jd#h23"));