Skip to content

Commit e8845c7

Browse files
author
Jeremy Grelle
committed
SPR-6340 - Add coverage of JSON support to reference guide
1 parent a5be9c5 commit e8845c7

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

spring-framework-reference/src/mvc.xml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,8 +2151,13 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
21512151
<classname>ViewResolvers</classname>. The first
21522152
<classname>View</classname> in the list that has a compatible
21532153
<literal>Content-Type</literal> returns the representation to the
2154-
client. The <literal>Accept</literal> header may include wildcards, for
2155-
example text/*, in which case a <classname>View</classname> whose
2154+
client. If a compatible view cannot be supplied by the
2155+
<classname>ViewResolver</classname> chain, then the list of views specified
2156+
through the <literal>DefaultViews</literal> property will be consulted. This
2157+
latter option is appropriate for singleton <classname>Views</classname> that
2158+
can render an appropriate representation of the current resource regardless
2159+
of the logical view name. The <literal>Accept</literal> header may include
2160+
wildcards, for example text/*, in which case a <classname>View</classname> whose
21562161
Context-Type was text/xml is a compatible match.</para>
21572162

21582163
<para>To support the resolution of a view based on a file extension,
@@ -2170,6 +2175,7 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
21702175
&lt;map&gt;
21712176
&lt;entry key="atom" value="application/atom+xml"/&gt;
21722177
&lt;entry key="html" value="text/html"/&gt;
2178+
&lt;entry key="json" value="application/json"/&gt;
21732179
&lt;/map&gt;
21742180
&lt;/property&gt;
21752181
&lt;property name="viewResolvers"&gt;
@@ -2181,6 +2187,11 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
21812187
&lt;/bean&gt;
21822188
&lt;/list&gt;
21832189
&lt;/property&gt;
2190+
&lt;property name="defaultViews">
2191+
&lt;list>
2192+
&lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
2193+
&lt;/list>
2194+
&lt;/property&gt;
21842195
&lt;/bean&gt;
21852196

21862197

@@ -2206,10 +2217,12 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
22062217
matches the <literal>application/atom+xml</literal> media type. This view is provided by
22072218
the <classname>BeanNameViewResolver</classname> that maps to the
22082219
<classname>SampleContentAtomView</classname> if the view name returned
2209-
is <classname>content</classname>. Alternatively, client requests can be
2210-
made without a file extension but with the <literal>Accept</literal> header set to the
2211-
preferred media-type, and the same resolution of request to views would
2212-
occur.<!--Can you reword preceding sentence? I don't follow it.--></para>
2220+
is <classname>content</classname>. If the request is made with the file extension
2221+
<literal>.json</literal>, the <classname>MappingJacksonJsonView</classname> instance from
2222+
the <literal>DefaultViews</literal> list will be selected regardless of the view name.
2223+
Alternatively, client requests can be made without a file extension but with the
2224+
<literal>Accept</literal> header set to the preferred media-type, and the same resolution
2225+
of request to views would occur.<!--Can you reword preceding sentence? I don't follow it.--></para>
22132226

22142227
<note>
22152228
<para>If <classname>ContentNegotiatingViewResolver</classname>'s list

spring-framework-reference/src/remoting.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,8 @@ if (HttpStatus.SC_CREATED == post.getStatusCode()) {
14141414
<classname>SourceHttpMessageConverter</classname>. You can override
14151415
these defaults using the <methodname>messageConverters</methodname> bean
14161416
property as would be required if using the
1417-
<classname>MarshallingHttpMessageConverter</classname>.</para>
1417+
<classname>MarshallingHttpMessageConverter</classname> or
1418+
<classname>MappingJacksonHttpMessageConverter</classname>.</para>
14181419

14191420
<para>Each method takes URI template arguments in two forms, either as a
14201421
<literal>String</literal> variable length argument or a
@@ -1577,6 +1578,20 @@ URI location = template.postForLocation(uri, booking, "1");
15771578
this converter supports (<literal>text/xml</literal>) and
15781579
(<literal>application/xml</literal>).</para>
15791580
</section>
1581+
1582+
<section id="rest-mapping-json-converter">
1583+
<title>MappingJacksonHttpMessageConverter</title>
1584+
1585+
<para>An <interfacename>HttpMessageConverter</interfacename>
1586+
implementation that can read and write JSON using Jackson's
1587+
<interfacename>ObjectMapper</interfacename>. JSON mapping can be
1588+
customized as needed through the use of Jackson's provided annotations. When
1589+
further control is needed, a custom
1590+
<interfacename>ObjectMapper</interfacename> can be injected through
1591+
the <literal>ObjectMapper</literal> property for cases where custom
1592+
JSON serializers/deserializers need to be provided for specific types.
1593+
By default this converter supports (<literal>application/json</literal>).</para>
1594+
</section>
15801595

15811596
<section id="rest-source-converter">
15821597
<title>SourceHttpMessageConverter</title>

spring-framework-reference/src/view.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,5 +2589,24 @@ simpleReport.reportDataKey=myBeanData</programlisting>
25892589
chapter <link linkend="oxm">Marshalling XML using O/X
25902590
Mappers</link>.</para>
25912591
</section>
2592+
2593+
<section id="view-json-mapping">
2594+
<title>JSON Mapping View</title>
2595+
2596+
<para>The <classname>MappingJacksonJsonView</classname> uses the Jackson
2597+
library's <classname>ObjectMapper</classname> to render the response content
2598+
as JSON. By default, the entire contents of the model map (with the exception
2599+
of framework-specific classes) will be encoded as JSON. For cases where the
2600+
contents of the map need to be filtered, users may specify a specific set of
2601+
model attributes to encode via the <literal>RenderedAttributes</literal>
2602+
property.</para>
2603+
2604+
<para>JSON mapping can be customized as needed through the use of Jackson's provided
2605+
annotations. When further control is needed, a custom
2606+
<interfacename>ObjectMapper</interfacename> can be injected through the
2607+
<literal>ObjectMapper</literal> property for cases where custom JSON
2608+
serializers/deserializers need to be provided for specific types.</para>
2609+
2610+
</section>
25922611

25932612
</chapter>

0 commit comments

Comments
 (0)