@@ -64,7 +64,7 @@ public class MappingJackson2JsonView extends AbstractView {
64
64
65
65
private JsonEncoding encoding = JsonEncoding .UTF8 ;
66
66
67
- private boolean prefixJson = false ;
67
+ private String jsonPrefix ;
68
68
69
69
private Boolean prettyPrint ;
70
70
@@ -122,16 +122,26 @@ public final JsonEncoding getEncoding() {
122
122
return this .encoding ;
123
123
}
124
124
125
+ /**
126
+ * Specify a custom prefix to use for this view's JSON output.
127
+ * Default is none.
128
+ * @see #setPrefixJson
129
+ */
130
+ public void setJsonPrefix (String jsonPrefix ) {
131
+ this .jsonPrefix = jsonPrefix ;
132
+ }
133
+
125
134
/**
126
135
* Indicates whether the JSON output by this view should be prefixed with <tt>"{} && "</tt>.
127
136
* Default is {@code false}.
128
137
* <p>Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
129
138
* The prefix renders the string syntactically invalid as a script so that it cannot be hijacked.
130
139
* This prefix does not affect the evaluation of JSON, but if JSON validation is performed
131
140
* on the string, the prefix would need to be ignored.
141
+ * @see #setJsonPrefix
132
142
*/
133
143
public void setPrefixJson (boolean prefixJson ) {
134
- this .prefixJson = prefixJson ;
144
+ this .jsonPrefix = "{} && " ;
135
145
}
136
146
137
147
/**
@@ -243,7 +253,7 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
243
253
244
254
OutputStream stream = (this .updateContentLength ? createTemporaryOutputStream () : response .getOutputStream ());
245
255
Object value = filterModel (model );
246
- writeContent (stream , value , this .prefixJson );
256
+ writeContent (stream , value , this .jsonPrefix );
247
257
if (this .updateContentLength ) {
248
258
writeToResponse (response , (ByteArrayOutputStream ) stream );
249
259
}
@@ -272,11 +282,11 @@ protected Object filterModel(Map<String, Object> model) {
272
282
* Write the actual JSON content to the stream.
273
283
* @param stream the output stream to use
274
284
* @param value the value to be rendered, as returned from {@link #filterModel}
275
- * @param prefixJson whether the JSON output by this view should be prefixed
276
- * with <tt>"{} && "</tt> (as indicated through {@link #setPrefixJson})
285
+ * @param jsonPrefix the prefix for this view's JSON output
286
+ * (as indicated through {@link #setJsonPrefix}/ {@link #setPrefixJson})
277
287
* @throws IOException if writing failed
278
288
*/
279
- protected void writeContent (OutputStream stream , Object value , boolean prefixJson ) throws IOException {
289
+ protected void writeContent (OutputStream stream , Object value , String jsonPrefix ) throws IOException {
280
290
// The following has been deprecated as late as Jackson 2.2 (April 2013);
281
291
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
282
292
@ SuppressWarnings ("deprecation" )
@@ -288,8 +298,8 @@ protected void writeContent(OutputStream stream, Object value, boolean prefixJso
288
298
generator .useDefaultPrettyPrinter ();
289
299
}
290
300
291
- if (prefixJson ) {
292
- generator .writeRaw ("{} && " );
301
+ if (jsonPrefix != null ) {
302
+ generator .writeRaw (jsonPrefix );
293
303
}
294
304
this .objectMapper .writeValue (generator , value );
295
305
}
0 commit comments