Skip to content

Commit 398729c

Browse files
committed
SPR-5853 - JSON formatting view for Spring MVC
1 parent 56b0606 commit 398729c

File tree

6 files changed

+507
-31
lines changed

6 files changed

+507
-31
lines changed

org.springframework.web.servlet/ivy.xml

+33-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<?xml-stylesheet type="text/xsl" href="http://ivyrep.jayasoft.org/ivy-doc.xsl"?>
33
<ivy-module
4-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:noNamespaceSchemaLocation="http://incubator.apache.org/ivy/schemas/ivy.xsd"
6-
version="1.3">
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://incubator.apache.org/ivy/schemas/ivy.xsd"
6+
version="1.3">
77

88
<info organisation="org.springframework" module="${ant.project.name}">
99
<license name="Apache 2.0" url="http://www.apache.org/licenses/LICENSE-2.0"/>
@@ -17,6 +17,7 @@
1717
<conf name="freemarker" extends="runtime" description="JARs needed to create beans for Freemarker"/>
1818
<conf name="itext" extends="runtime" description="JARs needed to create beans for iText"/>
1919
<conf name="jasper-reports" extends="runtime" description="JARs needed to create beans for Jasper Reports"/>
20+
<conf name="jackson" extends="runtime" description="JARs needed to use the Jackson JSON View"/>
2021
<conf name="jexcelapi" extends="runtime" description="JARs needed to create beans for JExcelApi"/>
2122
<conf name="oxm" extends="runtime" description="JARs needed to use the MarshallingMessageConverter"/>
2223
<conf name="poi" extends="runtime" description="JARs needed to create beans for Poi"/>
@@ -31,57 +32,62 @@
3132

3233
<dependencies>
3334
<dependency org="com.sun.syndication" name="com.springsource.com.sun.syndication" rev="1.0.0"
34-
conf="optional, feed->compile"/>
35+
conf="optional, feed->compile"/>
3536
<dependency org="com.lowagie.text" name="com.springsource.com.lowagie.text" rev="2.0.8"
36-
conf="optional, itext->compile"/>
37+
conf="optional, itext->compile"/>
3738
<dependency org="org.freemarker" name="com.springsource.freemarker" rev="2.3.15"
38-
conf="optional, freemarker->compile"/>
39+
conf="optional, freemarker->compile"/>
3940
<dependency org="javax.el" name="com.springsource.javax.el" rev="1.0.0" conf="provided->compile"/>
4041
<dependency org="javax.servlet" name="com.springsource.javax.servlet" rev="2.5.0" conf="provided->compile"/>
4142
<dependency org="javax.servlet" name="com.springsource.javax.servlet.jsp" rev="2.1.0" conf="provided->compile"/>
4243
<dependency org="javax.servlet" name="com.springsource.javax.servlet.jsp.jstl" rev="1.1.2"
43-
conf="provided->compile"/>
44+
conf="provided->compile"/>
4445
<dependency org="net.sourceforge.jexcelapi" name="com.springsource.jxl" rev="2.6.6"
45-
conf="optional, jexcelapi->compile"/>
46+
conf="optional, jexcelapi->compile"/>
4647
<dependency org="net.sourceforge.jasperreports" name="com.springsource.net.sf.jasperreports" rev="2.0.5"
47-
conf="optional, jasper-reports->compile"/>
48+
conf="optional, jasper-reports->compile"/>
4849
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.fileupload" rev="1.2.0"
49-
conf="optional, commons-fileupload->compile"/>
50+
conf="optional, commons-fileupload->compile"/>
5051
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1"
51-
conf="compile->compile"/>
52+
conf="compile->compile"/>
5253
<dependency org="org.apache.poi" name="com.springsource.org.apache.poi" rev="3.0.2.FINAL"
53-
conf="optional, poi->compile"/>
54+
conf="optional, poi->compile"/>
5455
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles" rev="2.0.5"
55-
conf="optional, tiles->compile"/>
56+
conf="optional, tiles->compile"/>
5657
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles.core" rev="2.0.5.osgi"
57-
conf="optional, tiles->compile"/>
58+
conf="optional, tiles->compile"/>
5859
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles.jsp" rev="2.0.5"
59-
conf="optional, tiles->compile"/>
60+
conf="optional, tiles->compile"/>
6061
<dependency org="org.apache.velocity" name="com.springsource.org.apache.velocity" rev="1.5.0"
61-
conf="optional, velocity->compile"/>
62+
conf="optional, velocity->compile"/>
6263
<dependency org="org.apache.velocity" name="com.springsource.org.apache.velocity.tools.view" rev="1.4.0"
63-
conf="optional, velocity->compile"/>
64+
conf="optional, velocity->compile"/>
65+
<dependency org="org.codehaus.jackson" name="com.springsource.org.codehaus.jackson.mapper" rev="1.0.0"
66+
conf="optional, jackson->compile"/>
6467
<dependency org="org.springframework" name="org.springframework.beans" rev="latest.integration"
65-
conf="compile->compile"/>
68+
conf="compile->compile"/>
6669
<dependency org="org.springframework" name="org.springframework.context" rev="latest.integration"
67-
conf="compile->compile"/>
70+
conf="compile->compile"/>
6871
<dependency org="org.springframework" name="org.springframework.context.support" rev="latest.integration"
69-
conf="optional, velocity, freemarker, jasper-reports->compile"/>
72+
conf="optional, velocity, freemarker, jasper-reports->compile"/>
7073
<dependency org="org.springframework" name="org.springframework.core" rev="latest.integration"
71-
conf="compile->compile"/>
74+
conf="compile->compile"/>
7275
<dependency org="org.springframework" name="org.springframework.oxm" rev="latest.integration"
73-
conf="optional, oxm->compile"/>
76+
conf="optional, oxm->compile"/>
7477
<dependency org="org.springframework" name="org.springframework.web" rev="latest.integration"
75-
conf="compile->compile"/>
76-
<!-- test dependencies -->
77-
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.5.0" conf="test->runtime"/>
78+
conf="compile->compile"/>
79+
<!-- test dependencies -->
80+
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.5.0" conf="test->runtime"/>
7881
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->compile"/>
79-
<dependency org="org.springframework" name="org.springframework.asm" rev="latest.integration" conf="test->compile"/>
82+
<dependency org="org.springframework" name="org.springframework.asm" rev="latest.integration"
83+
conf="test->compile"/>
8084
<dependency org="org.custommonkey.xmlunit" name="com.springsource.org.custommonkey.xmlunit" rev="1.2.0"
81-
conf="test->compile"/>
85+
conf="test->compile"/>
8286
<dependency org="org.dom4j" name="com.springsource.org.dom4j" rev="1.6.1" conf="test->compile"/>
8387
<dependency org="org.jaxen" name="com.springsource.org.jaxen" rev="1.1.1" conf="test->compile"/>
8488
<dependency org="net.sourceforge.cglib" name="com.springsource.net.sf.cglib" rev="2.1.3" conf="test->compile"/>
89+
<dependency org="org.mozilla.javascript" name="com.springsource.org.mozilla.javascript" rev="1.7.0.R2"
90+
conf="test->runtime"/>
8591

8692
</dependencies>
8793

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.view.json;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
import java.util.Set;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
25+
import org.codehaus.jackson.JsonEncoding;
26+
import org.codehaus.jackson.JsonGenerator;
27+
import org.codehaus.jackson.map.ObjectMapper;
28+
import org.codehaus.jackson.map.SerializerFactory;
29+
30+
import org.springframework.util.Assert;
31+
import org.springframework.util.CollectionUtils;
32+
import org.springframework.validation.BindingResult;
33+
import org.springframework.web.servlet.View;
34+
import org.springframework.web.servlet.view.AbstractView;
35+
36+
/**
37+
* Spring-MVC {@link View} that renders JSON content by serializing the model for the current request using <a
38+
* href="http://jackson.codehaus.org/">Jackson's</a> {@link ObjectMapper}.
39+
*
40+
* <p>By default, the entire contents of the model map (with the exception of framework-specific classes) will be
41+
* encoded as JSON. For cases where the contents of the map need to be filtered, users may specify a specific set of
42+
* model attributes to encode via the {@link #setRenderedAttributes(Set) includeAttributes} property.
43+
*
44+
* @author Jeremy Grelle
45+
* @author Arjen Poutsma
46+
* @see org.springframework.http.converter.json.BindingJacksonHttpMessageConverter
47+
* @since 3.0
48+
*/
49+
public class BindingJacksonJsonView extends AbstractView {
50+
51+
/**
52+
* Default content type. Overridable as bean property.
53+
*/
54+
public static final String DEFAULT_CONTENT_TYPE = "application/json";
55+
56+
private ObjectMapper objectMapper = new ObjectMapper();
57+
58+
private JsonEncoding encoding = JsonEncoding.UTF8;
59+
60+
private boolean prefixJson = false;
61+
62+
private Set<String> renderedAttributes;
63+
64+
/**
65+
* Construct a new {@code JacksonJsonView}, setting the content type to {@code application/json}.
66+
*/
67+
public BindingJacksonJsonView() {
68+
setContentType(DEFAULT_CONTENT_TYPE);
69+
}
70+
71+
/**
72+
* Sets the {@code ObjectMapper} for this view. If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper}
73+
* is used.
74+
*
75+
* <p>Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON serialization
76+
* process. For example, an extended {@link SerializerFactory} can be configured that provides custom serializers for
77+
* specific types. The other option for refining the serialization process is to use Jackson's provided annotations on
78+
* the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.
79+
*/
80+
public void setObjectMapper(ObjectMapper objectMapper) {
81+
Assert.notNull(objectMapper, "'objectMapper' must not be null");
82+
this.objectMapper = objectMapper;
83+
}
84+
85+
/**
86+
* Sets the {@code JsonEncoding} for this converter. By default, {@linkplain JsonEncoding#UTF8 UTF-8} is used.
87+
*/
88+
public void setEncoding(JsonEncoding encoding) {
89+
Assert.notNull(encoding, "'encoding' must not be null");
90+
this.encoding = encoding;
91+
}
92+
93+
/**
94+
* Indicates whether the JSON output by this view should be prefixed with "{@code {} &&}". Default is false.
95+
*
96+
* <p> Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string
97+
* syntactically invalid as a script so that it cannot be hijacked. This prefix does not affect the evaluation of JSON,
98+
* but if JSON validation is performed on the string, the prefix would need to be ignored.
99+
*/
100+
public void setPrefixJson(boolean prefixJson) {
101+
this.prefixJson = prefixJson;
102+
}
103+
104+
/**
105+
* Sets the attributes in the model that should be rendered by this view. When set, all other model attributes will be
106+
* ignored.
107+
*/
108+
public void setRenderedAttributes(Set<String> renderedAttributes) {
109+
this.renderedAttributes = renderedAttributes;
110+
}
111+
112+
@Override
113+
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
114+
response.setContentType(getContentType());
115+
response.setCharacterEncoding(encoding.getJavaName());
116+
}
117+
118+
@Override
119+
protected void renderMergedOutputModel(Map<String, Object> model,
120+
HttpServletRequest request,
121+
HttpServletResponse response) throws Exception {
122+
model = filterModel(model);
123+
JsonGenerator generator =
124+
objectMapper.getJsonFactory().createJsonGenerator(response.getOutputStream(), encoding);
125+
if (prefixJson) {
126+
generator.writeRaw("{} && ");
127+
}
128+
objectMapper.writeValue(generator, model);
129+
}
130+
131+
/**
132+
* Filters out undesired attributes from the given model.
133+
*
134+
* <p>Default implementation removes {@link BindingResult} instances and entries not included in the {@link
135+
* #setRenderedAttributes(Set) renderedAttributes} property.
136+
*/
137+
protected Map<String, Object> filterModel(Map<String, Object> model) {
138+
Map<String, Object> result = new HashMap<String, Object>(model.size());
139+
if (CollectionUtils.isEmpty(renderedAttributes)) {
140+
renderedAttributes = model.keySet();
141+
}
142+
for (Map.Entry<String, Object> entry : model.entrySet()) {
143+
if (!(entry instanceof BindingResult) && renderedAttributes.contains(entry.getKey())) {
144+
result.put(entry.getKey(), entry.getValue());
145+
}
146+
}
147+
return result;
148+
}
149+
150+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
*
19+
* Support classes for providing a View implementation based on JSON serialization.
20+
*
21+
*/
22+
package org.springframework.web.servlet.view.json;
23+

0 commit comments

Comments
 (0)