Skip to content

Commit 7bf9b76

Browse files
committed
Improve HTTP message reading/writing docs
Cross-reference sections on HttpMessageReader/Writer and HttpMessageConverter and improve the content. Issue: SPR-16260
1 parent 0f1f95e commit 7bf9b76

File tree

3 files changed

+51
-66
lines changed

3 files changed

+51
-66
lines changed

src/docs/asciidoc/integration.adoc

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,39 +1232,18 @@ to serialize only a subset of the object properties. For example:
12321232

12331233

12341234
[[rest-message-conversion]]
1235-
==== HTTP message conversion
1235+
==== HTTP Message Converters
1236+
[.small]#<<web-reactive.adoc#webflux-codecs,Same in Spring WebFlux>>#
12361237

1237-
Objects passed to and returned from the methods `getForObject()`, `postForLocation()`,
1238-
and `put()` are converted to HTTP requests and from HTTP responses by
1239-
`HttpMessageConverters`. The `HttpMessageConverter` interface is shown below to give you
1240-
a better feel for its functionality
1241-
1242-
[source,java,indent=0]
1243-
[subs="verbatim,quotes"]
1244-
----
1245-
public interface HttpMessageConverter<T> {
1246-
1247-
// Indicate whether the given class and media type can be read by this converter.
1248-
boolean canRead(Class<?> clazz, MediaType mediaType);
1249-
1250-
// Indicate whether the given class and media type can be written by this converter.
1251-
boolean canWrite(Class<?> clazz, MediaType mediaType);
1252-
1253-
// Return the list of MediaType objects supported by this converter.
1254-
List<MediaType> getSupportedMediaTypes();
1255-
1256-
// Read an object of the given type from the given input message, and returns it.
1257-
T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;
1258-
1259-
// Write an given object to the given output message.
1260-
void write(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;
1261-
1262-
}
1263-
----
1238+
The `spring-web` module contains the `HttpMessageConverter` contract for reading and
1239+
writing the body of HTTP requests and responses via `InputStream` and `OutputStream`.
1240+
``HttpMessageConverter``'s are used on the client side, e.g. in the `RestTemplate`, and
1241+
also on the server side, e.g. in Spring MVC REST controllers.
12641242

12651243
Concrete implementations for the main media (mime) types are provided in the framework
12661244
and are registered by default with the `RestTemplate` on the client-side and with
1267-
`RequestMethodHandlerAdapter` on the server-side.
1245+
`RequestMethodHandlerAdapter` on the server-side (see
1246+
<<web.adoc#mvc-config-message-converters,Configuring Message Converters>>).
12681247

12691248
The implementations of ``HttpMessageConverter``s are described in the following sections.
12701249
For all converters a default media type is used but can be overridden by setting the

src/docs/asciidoc/web/webflux.adoc

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ of RxJava or other reactive library. See <<webflux-reactive-libraries>> for more
110110
[[webflux-programming-models]]
111111
=== Programming models
112112

113-
The `spring-web` module contains the reactive foundation that underlies Spring WebFlux --
114-
HTTP abstractions, Reactive Streams server adapters, reactive codecs, and a
115-
core Web API whose role is comparable to the Servlet API but with non-blocking semantics.
113+
The `spring-web` module contains the reactive foundation that underlies Spring WebFlux
114+
including HTTP abstractions, Reactive Streams <<webflux-httphandler,adapters>> for supported
115+
servers, <<webflux-codecs,codecs>>, and a core <<webflux-web-handler-api>> comparable to
116+
the Servlet API but with non-blocking contracts.
116117

117118
On that foundation Spring WebFlux provides a choice of two programming models:
118119

@@ -425,31 +426,34 @@ to have the following detected:
425426

426427

427428
[[webflux-codecs]]
428-
=== Codecs
429+
=== HTTP Message Codecs
430+
[.small]#<<integration.adoc#rest-message-conversion,Same in Spring MVC>>#
429431

430-
The `spring-web` module provides
432+
The `spring-web` module defines the
431433
{api-spring-framework}/http/codec/HttpMessageReader.html[HttpMessageReader] and
432-
{api-spring-framework}/http/codec/HttpMessageWriter.html[HttpMessageWriter]
433-
for encoding and decoding the HTTP request and response body with Reactive Streams.
434-
It builds on lower level contracts from `spring-core`:
435-
436-
* {api-spring-framework}/core/io/buffer/DataBuffer.html[DataBuffer] -- abstraction for
437-
byte buffers -- e.g. Netty `ByteBuf`, `java.nio.ByteBuffer`, see
438-
<<core#databuffers, Data Buffers and Codecs>>.
439-
* {api-spring-framework}/core/codec/Encoder.html[Encoder] -- serialize a stream of Objects
440-
to a stream of data buffers
441-
* {api-spring-framework}/core/codec/Decoder.html[Decoder] -- deserialize a stream of data
442-
buffers into a stream of Objects
443-
444-
Basic `Encoder` and `Decoder` implementations exist in `spring-core` but `spring-web` adds
445-
more for JSON, XML, and other formats. You can wrap any `Encoder` and `Decoder` as a reader
446-
or writer with `EncoderHttpMessageWriter` and `DecoderHttpMessageReader`. There are some
447-
additional, web-only reader and writer implementations for server-sent events, form data,
448-
and more.
449-
450-
Finally, `ClientCodecConfigurer` and `ServerCodecConfigurer` can be used to initialize
451-
a list of readers and writers. They include support for classpath detection and a
452-
of defaults along with the ability to override or replace those defaults.
434+
{api-spring-framework}/http/codec/HttpMessageWriter.html[HttpMessageWriter] contracts
435+
for encoding and decoding the body of HTTP requests and responses via Rective Streams
436+
``Publisher``'s. These contacts are used on the client side, e.g. in the `WebClient`,
437+
and on the server side, e.g. in annotated controllers and functional endpoints.
438+
439+
The `spring-core` module defines the
440+
{api-spring-framework}/core/codec/Encoder.html[Encoder] and
441+
{api-spring-framework}/core/codec/Decoder.html[Decoder] contracts that are independent of
442+
HTTP and rely on the {api-spring-framework}/core/io/buffer/DataBuffer.html[DataBuffer]
443+
contract that abstracts different byte buffer representations such as the Netty `ByteBuf`
444+
and `java.nio.ByteBuffer` (see <<core#databuffers, Data Buffers and Codecs>>).
445+
An `Encoder` can be wrapped with `EncoderHttpMessageWriter` to be used as an
446+
`HttpMessageWriter` while a `Decoder` can be wrapped with `DecoderHttpMessageReader` to
447+
be used as an `HttpMessageReader`.
448+
449+
The `spring-core` module contains basic `Encoder` and `Decoder` implementations for
450+
`byte[]`, `ByteBuffer`, `DataBuffer`, `Resource`, and `String`. The `spring-web` module
451+
adds ``Encoder``'s and ``Decoder``'s for Jackson JSON, Jackson Smile, and JAXB2.
452+
The `spring-web` module also contains some web-specific readers and writers for
453+
server-sent events, form data, and multipart requests.
454+
455+
To configure or customize the readers and writers to use applications will typically use
456+
`ClientCodecConfigurer` or `ServerCodecConfigurer`.
453457

454458

455459

src/docs/asciidoc/web/webmvc.adoc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,8 @@ You could access the part named "meta-data" with a `@RequestParam("meta-data") S
22742274
metadata` controller method argument. However, you would probably prefer to accept a
22752275
strongly typed object initialized from the JSON formatted data in the body of the
22762276
request part, very similar to the way `@RequestBody` converts the body of a
2277-
non-multipart request to a target object with the help of an `HttpMessageConverter`.
2277+
non-multipart request to a target object with the help of an
2278+
<<integration.adoc#rest-message-conversion,HttpMessageConverter>>.
22782279

22792280
You can use the `@RequestPart` annotation instead of the `@RequestParam` annotation for
22802281
this purpose. It allows you to have the content of a specific multipart passed through
@@ -2314,7 +2315,8 @@ be bound to the value of the HTTP request body. For example:
23142315
}
23152316
----
23162317

2317-
You convert the request body to the method argument by using an `HttpMessageConverter`.
2318+
You convert the request body to the method argument by using an
2319+
<<integration.adoc#rest-message-conversion,HttpMessageConverter>>.
23182320
`HttpMessageConverter` is responsible for converting from the HTTP request message to an
23192321
object and converting from an object to the HTTP response body. The
23202322
`RequestMappingHandlerAdapter` supports the `@RequestBody` annotation with the following
@@ -2422,8 +2424,7 @@ The above example will result in the text `Hello World` being written to the HTT
24222424
response stream.
24232425

24242426
As with `@RequestBody`, Spring converts the returned object to a response body by using
2425-
an `HttpMessageConverter`. For more information on these converters, see the previous
2426-
section and <<integration.adoc#rest-message-conversion,Message Converters>>.
2427+
an <<integration.adoc#rest-message-conversion,HttpMessageConverter>>.
24272428

24282429

24292430
[[mvc-ann-responseentity]]
@@ -2443,7 +2444,8 @@ also allows setting response headers:
24432444
}
24442445
----
24452446

2446-
As with `@ResponseBody`, Spring uses `HttpMessageConverter` to
2447+
As with `@ResponseBody`, Spring uses
2448+
<<integration.adoc#rest-message-conversion,HttpMessageConverter>> to
24472449
convert from and to the request and response streams. For more information on these
24482450
converters, see the previous section and <<integration.adoc#rest-message-conversion,
24492451
Message Converters>>.
@@ -3353,7 +3355,7 @@ This is a technique related to "Long Polling" that is known as "HTTP Streaming".
33533355
Spring MVC makes this possible through the `ResponseBodyEmitter` return value
33543356
type which can be used to send multiple Objects, instead of one as is normally
33553357
the case with `@ResponseBody`, where each Object sent is written to the
3356-
response with an `HttpMessageConverter`.
3358+
response with an <<integration.adoc#rest-message-conversion,HttpMessageConverter>>.
33573359

33583360
Here is an example of that:
33593361

@@ -3412,11 +3414,11 @@ http://blog.pivotal.io/pivotal/products/websocket-architecture-in-spring-4-0[the
34123414
=== Streaming raw data
34133415

34143416
`ResponseBodyEmitter` allows sending events by writing Objects to the
3415-
response through an `HttpMessageConverter`. This is probably the most common
3416-
case, for example when writing JSON data. However sometimes it is useful to
3417-
bypass message conversion and write directly to the response `OutputStream`
3418-
for example for a file download. This can be done with the help of the
3419-
`StreamingResponseBody` return value type.
3417+
response through an <<integration.adoc#rest-message-conversion,HttpMessageConverter>>.
3418+
This is probably the most common case, for example when writing JSON data.
3419+
However sometimes it is useful to bypass message conversion and write directly to the
3420+
response `OutputStream` for example for a file download. This can be done with the help
3421+
of the `StreamingResponseBody` return value type.
34203422

34213423
Here is an example of that:
34223424

0 commit comments

Comments
 (0)