Skip to content

Commit d0d670c

Browse files
committed
Make the JSON prefix used in converters configurable
Issue: SPR-10627
1 parent a4c15d6 commit d0d670c

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@
2020
import java.lang.reflect.Type;
2121
import java.nio.charset.Charset;
2222

23-
import com.fasterxml.jackson.core.JsonEncoding;
24-
import com.fasterxml.jackson.core.JsonGenerator;
25-
import com.fasterxml.jackson.core.JsonProcessingException;
26-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
27-
import com.fasterxml.jackson.databind.JavaType;
28-
import com.fasterxml.jackson.databind.ObjectMapper;
29-
import com.fasterxml.jackson.databind.SerializationFeature;
30-
3123
import org.springframework.http.HttpInputMessage;
3224
import org.springframework.http.HttpOutputMessage;
3325
import org.springframework.http.MediaType;
@@ -37,6 +29,14 @@
3729
import org.springframework.http.converter.HttpMessageNotWritableException;
3830
import org.springframework.util.Assert;
3931

32+
import com.fasterxml.jackson.core.JsonEncoding;
33+
import com.fasterxml.jackson.core.JsonGenerator;
34+
import com.fasterxml.jackson.core.JsonProcessingException;
35+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
36+
import com.fasterxml.jackson.databind.JavaType;
37+
import com.fasterxml.jackson.databind.ObjectMapper;
38+
import com.fasterxml.jackson.databind.SerializationFeature;
39+
4040
/**
4141
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that
4242
* can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson 2.x's</a> {@link ObjectMapper}.
@@ -61,7 +61,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
6161

6262
private ObjectMapper objectMapper = new ObjectMapper();
6363

64-
private boolean prefixJson = false;
64+
private String jsonPrefix;
6565

6666
private Boolean prettyPrint;
6767

@@ -97,15 +97,25 @@ public ObjectMapper getObjectMapper() {
9797
return this.objectMapper;
9898
}
9999

100+
/**
101+
* Specify a custom prefix to use for this view's JSON output.
102+
* Default is none.
103+
* @see #setPrefixJson
104+
*/
105+
public void setJsonPrefix(String jsonPrefix) {
106+
this.jsonPrefix = jsonPrefix;
107+
}
108+
100109
/**
101110
* Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false.
102111
* <p>Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
103112
* The prefix renders the string syntactically invalid as a script so that it cannot be hijacked.
104113
* This prefix does not affect the evaluation of JSON, but if JSON validation is performed on the
105114
* string, the prefix would need to be ignored.
115+
* @see #setJsonPrefix
106116
*/
107117
public void setPrefixJson(boolean prefixJson) {
108-
this.prefixJson = prefixJson;
118+
this.jsonPrefix = prefixJson ? "{} && " : null;
109119
}
110120

111121
/**
@@ -194,7 +204,7 @@ protected void writeInternal(Object object, HttpOutputMessage outputMessage)
194204
}
195205

196206
try {
197-
if (this.prefixJson) {
207+
if (this.jsonPrefix != null) {
198208
jsonGenerator.writeRaw("{} && ");
199209
}
200210
this.objectMapper.writeValue(jsonGenerator, object);

spring-web/src/main/java/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.codehaus.jackson.map.ObjectMapper;
2727
import org.codehaus.jackson.map.SerializationConfig;
2828
import org.codehaus.jackson.type.JavaType;
29-
3029
import org.springframework.http.HttpInputMessage;
3130
import org.springframework.http.HttpOutputMessage;
3231
import org.springframework.http.MediaType;
@@ -59,7 +58,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
5958

6059
private ObjectMapper objectMapper = new ObjectMapper();
6160

62-
private boolean prefixJson = false;
61+
private String jsonPrefix;
6362

6463
private Boolean prettyPrint;
6564

@@ -95,15 +94,25 @@ public ObjectMapper getObjectMapper() {
9594
return this.objectMapper;
9695
}
9796

97+
/**
98+
* Specify a custom prefix to use for this view's JSON output.
99+
* Default is none.
100+
* @see #setPrefixJson
101+
*/
102+
public void setJsonPrefix(String jsonPrefix) {
103+
this.jsonPrefix = jsonPrefix;
104+
}
105+
98106
/**
99107
* Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false.
100108
* <p>Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
101109
* The prefix renders the string syntactically invalid as a script so that it cannot be hijacked.
102110
* This prefix does not affect the evaluation of JSON, but if JSON validation is performed on the
103111
* string, the prefix would need to be ignored.
112+
* @see #setJsonPrefix
104113
*/
105114
public void setPrefixJson(boolean prefixJson) {
106-
this.prefixJson = prefixJson;
115+
this.jsonPrefix = prefixJson ? "{} && " : null;
107116
}
108117

109118
/**
@@ -190,7 +199,7 @@ protected void writeInternal(Object object, HttpOutputMessage outputMessage)
190199
}
191200

192201
try {
193-
if (this.prefixJson) {
202+
if (this.jsonPrefix != null) {
194203
jsonGenerator.writeRaw("{} && ");
195204
}
196205
this.objectMapper.writeValue(jsonGenerator, object);

spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void setJsonPrefix(String jsonPrefix) {
142142
* @see #setJsonPrefix
143143
*/
144144
public void setPrefixJson(boolean prefixJson) {
145-
this.jsonPrefix = prefixJson ? "{} && " : "";
145+
this.jsonPrefix = prefixJson ? "{} && " : null;
146146
}
147147

148148
/**

spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void setJsonPrefix(String jsonPrefix) {
141141
* @see #setJsonPrefix
142142
*/
143143
public void setPrefixJson(boolean prefixJson) {
144-
this.jsonPrefix = prefixJson ? "{} && " : "";
144+
this.jsonPrefix = prefixJson ? "{} && " : null;
145145
}
146146

147147
/**

0 commit comments

Comments
 (0)