diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java index 29c670a1389..8488aabe6dc 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java @@ -65,7 +65,7 @@ public class MappingJackson2JsonView extends AbstractView { private JsonEncoding encoding = JsonEncoding.UTF8; - private boolean prefixJson = false; + private String jsonPrefix; private Boolean prettyPrint; @@ -123,6 +123,15 @@ public class MappingJackson2JsonView extends AbstractView { return this.encoding; } + /** + * Specify a custom prefix to use for this view's JSON output. + * Default is none. + * @see #setPrefixJson + */ + public void setJsonPrefix(String jsonPrefix) { + this.jsonPrefix = jsonPrefix; + } + /** * Indicates whether the JSON output by this view should be prefixed with "{} && ". * Default is {@code false}. @@ -130,15 +139,16 @@ public class MappingJackson2JsonView extends AbstractView { * The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. * This prefix does not affect the evaluation of JSON, but if JSON validation is performed * on the string, the prefix would need to be ignored. + * @see #setJsonPrefix */ public void setPrefixJson(boolean prefixJson) { - this.prefixJson = prefixJson; + this.jsonPrefix = "{} && "; } /** * Whether to use the default pretty printer when writing JSON. * This is a shortcut for setting up an {@code ObjectMapper} as follows: - *
+ ** ObjectMapper mapper = new ObjectMapper(); * mapper.configure(SerializationFeature.INDENT_OUTPUT, true); *@@ -244,7 +254,7 @@ public class MappingJackson2JsonView extends AbstractView { OutputStream stream = (this.updateContentLength ? createTemporaryOutputStream() : response.getOutputStream()); Object value = filterModel(model); - writeContent(stream, value, this.prefixJson); + writeContent(stream, value, this.jsonPrefix); if (this.updateContentLength) { writeToResponse(response, (ByteArrayOutputStream) stream); } @@ -273,11 +283,11 @@ public class MappingJackson2JsonView extends AbstractView { * Write the actual JSON content to the stream. * @param stream the output stream to use * @param value the value to be rendered, as returned from {@link #filterModel} - * @param prefixJson whether the JSON output by this view should be prefixed - * with "{} && " (as indicated through {@link #setPrefixJson}) + * @param jsonPrefix the prefix for this view's JSON output + * (as indicated through {@link #setJsonPrefix}/{@link #setPrefixJson}) * @throws IOException if writing failed */ - protected void writeContent(OutputStream stream, Object value, boolean prefixJson) throws IOException { + protected void writeContent(OutputStream stream, Object value, String jsonPrefix) throws IOException { JsonGenerator generator = this.objectMapper.getJsonFactory().createJsonGenerator(stream, this.encoding); // A workaround for JsonGenerators not applying serialization features @@ -286,8 +296,8 @@ public class MappingJackson2JsonView extends AbstractView { generator.useDefaultPrettyPrinter(); } - if (prefixJson) { - generator.writeRaw("{} && "); + if (jsonPrefix != null) { + generator.writeRaw(jsonPrefix); } this.objectMapper.writeValue(generator, value); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java index ef0ad59d995..8bef94e9258 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java @@ -65,7 +65,7 @@ public class MappingJacksonJsonView extends AbstractView { private JsonEncoding encoding = JsonEncoding.UTF8; - private boolean prefixJson = false; + private String jsonPrefix; private Boolean prettyPrint; @@ -123,6 +123,15 @@ public class MappingJacksonJsonView extends AbstractView { return this.encoding; } + /** + * Specify a custom prefix to use for this view's JSON output. + * Default is none. + * @see #setPrefixJson + */ + public void setJsonPrefix(String jsonPrefix) { + this.jsonPrefix = jsonPrefix; + } + /** * Indicates whether the JSON output by this view should be prefixed with "{} && ". * Default is {@code false}. @@ -130,9 +139,10 @@ public class MappingJacksonJsonView extends AbstractView { * The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. * This prefix does not affect the evaluation of JSON, but if JSON validation is performed * on the string, the prefix would need to be ignored. + * @see #setJsonPrefix */ public void setPrefixJson(boolean prefixJson) { - this.prefixJson = prefixJson; + this.jsonPrefix = "{} && "; } /** @@ -244,7 +254,7 @@ public class MappingJacksonJsonView extends AbstractView { OutputStream stream = (this.updateContentLength ? createTemporaryOutputStream() : response.getOutputStream()); Object value = filterModel(model); - writeContent(stream, value, this.prefixJson); + writeContent(stream, value, this.jsonPrefix); if (this.updateContentLength) { writeToResponse(response, (ByteArrayOutputStream) stream); } @@ -273,11 +283,11 @@ public class MappingJacksonJsonView extends AbstractView { * Write the actual JSON content to the stream. * @param stream the output stream to use * @param value the value to be rendered, as returned from {@link #filterModel} - * @param prefixJson whether the JSON output by this view should be prefixed - * with "{} && " (as indicated through {@link #setPrefixJson}) + * @param jsonPrefix the prefix for this view's JSON output + * (as indicated through {@link #setJsonPrefix}/{@link #setPrefixJson}) * @throws IOException if writing failed */ - protected void writeContent(OutputStream stream, Object value, boolean prefixJson) throws IOException { + protected void writeContent(OutputStream stream, Object value, String jsonPrefix) throws IOException { JsonGenerator generator = this.objectMapper.getJsonFactory().createJsonGenerator(stream, this.encoding); // A workaround for JsonGenerators not applying serialization features @@ -286,8 +296,8 @@ public class MappingJacksonJsonView extends AbstractView { generator.useDefaultPrettyPrinter(); } - if (prefixJson) { - generator.writeRaw("{} && "); + if (jsonPrefix != null) { + generator.writeRaw(jsonPrefix); } this.objectMapper.writeValue(generator, value); }