From 3c80ee57dce46204298b49c3cd203ace1ccfdd57 Mon Sep 17 00:00:00 2001 From: Jeremy Grelle Date: Fri, 13 Nov 2009 02:04:22 +0000 Subject: [PATCH] SPR-6340 - Add coverage of JSON support to reference guide git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2427 50f2f4bb-b051-0410-bef5-90022cba6387 --- spring-framework-reference/src/mvc.xml | 25 ++++++++++++++++----- spring-framework-reference/src/remoting.xml | 17 +++++++++++++- spring-framework-reference/src/view.xml | 19 ++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/spring-framework-reference/src/mvc.xml b/spring-framework-reference/src/mvc.xml index 1abfdb5ff67..ff88fba885a 100644 --- a/spring-framework-reference/src/mvc.xml +++ b/spring-framework-reference/src/mvc.xml @@ -2151,8 +2151,13 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter { ViewResolvers. The first View in the list that has a compatible Content-Type returns the representation to the - client. The Accept header may include wildcards, for - example text/*, in which case a View whose + client. If a compatible view cannot be supplied by the + ViewResolver chain, then the list of views specified + through the DefaultViews property will be consulted. This + latter option is appropriate for singleton Views that + can render an appropriate representation of the current resource regardless + of the logical view name. The Accept header may include + wildcards, for example text/*, in which case a View whose Context-Type was text/xml is a compatible match. To support the resolution of a view based on a file extension, @@ -2170,6 +2175,7 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter { <map> <entry key="atom" value="application/atom+xml"/> <entry key="html" value="text/html"/> + <entry key="json" value="application/json"/> </map> </property> <property name="viewResolvers"> @@ -2181,6 +2187,11 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter { </bean> </list> </property> + <property name="defaultViews"> + <list> + <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> + </list> + </property> </bean> @@ -2206,10 +2217,12 @@ public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter { matches the application/atom+xml media type. This view is provided by the BeanNameViewResolver that maps to the SampleContentAtomView if the view name returned - is content. Alternatively, client requests can be - made without a file extension but with the Accept header set to the - preferred media-type, and the same resolution of request to views would - occur. + is content. If the request is made with the file extension + .json, the MappingJacksonJsonView instance from + the DefaultViews list will be selected regardless of the view name. + Alternatively, client requests can be made without a file extension but with the + Accept header set to the preferred media-type, and the same resolution + of request to views would occur. If ContentNegotiatingViewResolver's list diff --git a/spring-framework-reference/src/remoting.xml b/spring-framework-reference/src/remoting.xml index b0d3edfc2aa..d7510ab2341 100644 --- a/spring-framework-reference/src/remoting.xml +++ b/spring-framework-reference/src/remoting.xml @@ -1414,7 +1414,8 @@ if (HttpStatus.SC_CREATED == post.getStatusCode()) { SourceHttpMessageConverter. You can override these defaults using the messageConverters bean property as would be required if using the - MarshallingHttpMessageConverter. + MarshallingHttpMessageConverter or + MappingJacksonHttpMessageConverter. Each method takes URI template arguments in two forms, either as a String variable length argument or a @@ -1577,6 +1578,20 @@ URI location = template.postForLocation(uri, booking, "1"); this converter supports (text/xml) and (application/xml). + +
+ MappingJacksonHttpMessageConverter + + 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). +
SourceHttpMessageConverter diff --git a/spring-framework-reference/src/view.xml b/spring-framework-reference/src/view.xml index 26b438071a4..8c6831f4c1f 100644 --- a/spring-framework-reference/src/view.xml +++ b/spring-framework-reference/src/view.xml @@ -2589,5 +2589,24 @@ simpleReport.reportDataKey=myBeanData chapter Marshalling XML using O/X Mappers.
+ +
+ JSON Mapping View + + The MappingJacksonJsonView uses the Jackson + library's ObjectMapper to render the response content + as JSON. By default, the entire contents of the model map (with the exception + of framework-specific classes) will be encoded as JSON. For cases where the + contents of the map need to be filtered, users may specify a specific set of + model attributes to encode via the RenderedAttributes + property. + + 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. + +