Introduced "jsonPrefix" bean property
This change involves a modification of the "writeContent" template method to include the "jsonPrefix" String instead of the "prefixJson" boolean flag. Since said template method has only been introduced in 3.2.2, this change should hopefully not be a problem.
Issue: SPR-10567
(cherry picked from commit 28164b4
)
This commit is contained in:
parent
a8adec7673
commit
b6e20a4cc9
|
@ -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 <tt>"{} && "</tt>.
|
||||
* 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:
|
||||
* <pre>
|
||||
* <pre class="code">
|
||||
* ObjectMapper mapper = new ObjectMapper();
|
||||
* mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
|
||||
* </pre>
|
||||
|
@ -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 <tt>"{} && "</tt> (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);
|
||||
}
|
||||
|
|
|
@ -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 <tt>"{} && "</tt>.
|
||||
* 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 <tt>"{} && "</tt> (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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue