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 7510ab304e6..29c670a1389 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.web.servlet.view.json; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.OutputStream; import java.util.Collections; import java.util.HashMap; @@ -27,7 +28,6 @@ import javax.servlet.http.HttpServletResponse; import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -48,13 +48,15 @@ import org.springframework.web.servlet.view.AbstractView; * @author Jeremy Grelle * @author Arjen Poutsma * @author Rossen Stoyanchev + * @author Juergen Hoeller * @since 3.1.2 * @see org.springframework.http.converter.json.MappingJackson2HttpMessageConverter */ public class MappingJackson2JsonView extends AbstractView { /** - * Default content type. Overridable as bean property. + * Default content type: "application/json". + * Overridable through {@link #setContentType}. */ public static final String DEFAULT_CONTENT_TYPE = "application/json"; @@ -75,8 +77,9 @@ public class MappingJackson2JsonView extends AbstractView { private boolean updateContentLength = false; + /** - * Construct a new {@code JacksonJsonView}, setting the content type to {@code application/json}. + * Construct a new {@code MappingJackson2JsonView}, setting the content type to {@code application/json}. */ public MappingJackson2JsonView() { setContentType(DEFAULT_CONTENT_TYPE); @@ -85,13 +88,11 @@ public class MappingJackson2JsonView extends AbstractView { /** - * Sets the {@code ObjectMapper} for this view. - * If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used. - *
Setting a custom-configured {@code ObjectMapper} is one way to take further control - * of the JSON serialization process. For example, an extended {@code SerializerFactory} - * can be configured that provides custom serializers for specific types. The other option - * for refining the serialization process is to use Jackson's provided annotations on the - * types to be serialized, in which case a custom-configured ObjectMapper is unnecessary. + * Set the {@code ObjectMapper} for this view. + * If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} will be used. + *
Setting a custom-configured {@code ObjectMapper} is one way to take further control of + * the JSON serialization process. The other option is to use Jackson's provided annotations + * on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary. */ public void setObjectMapper(ObjectMapper objectMapper) { Assert.notNull(objectMapper, "'objectMapper' must not be null"); @@ -99,14 +100,15 @@ public class MappingJackson2JsonView extends AbstractView { configurePrettyPrint(); } - private void configurePrettyPrint() { - if (this.prettyPrint != null) { - this.objectMapper.configure(SerializationFeature.INDENT_OUTPUT, this.prettyPrint); - } + /** + * Return the {@code ObjectMapper} for this view. + */ + public final ObjectMapper getObjectMapper() { + return this.objectMapper; } /** - * Set the {@code JsonEncoding} for this converter. + * Set the {@code JsonEncoding} for this view. * By default, {@linkplain JsonEncoding#UTF8 UTF-8} is used. */ public void setEncoding(JsonEncoding encoding) { @@ -114,9 +116,16 @@ public class MappingJackson2JsonView extends AbstractView { this.encoding = encoding; } + /** + * Return the {@code JsonEncoding} for this view. + */ + public final JsonEncoding getEncoding() { + return this.encoding; + } + /** * Indicates whether the JSON output by this view should be prefixed with "{} && ". - * Default is false. + * Default is {@code false}. *
Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. * 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 @@ -127,12 +136,11 @@ public class MappingJackson2JsonView extends AbstractView { } /** - * Whether to use the {@link DefaultPrettyPrinter} when writing JSON. + * 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); - * converter.setObjectMapper(mapper); **
The default value is {@code false}.
*/
@@ -141,6 +149,12 @@ public class MappingJackson2JsonView extends AbstractView {
configurePrettyPrint();
}
+ private void configurePrettyPrint() {
+ if (this.prettyPrint != null) {
+ this.objectMapper.configure(SerializationFeature.INDENT_OUTPUT, this.prettyPrint);
+ }
+ }
+
/**
* Set the attribute in the model that should be rendered by this view.
* When set, all other model attributes will be ignored.
@@ -160,7 +174,7 @@ public class MappingJackson2JsonView extends AbstractView {
/**
* Return the attributes in the model that should be rendered by this view.
*/
- public Set The default implementation removes {@link BindingResult} instances and entries
* not included in the {@link #setRenderedAttributes renderedAttributes} property.
* @param model the model, as passed on to {@link #renderMergedOutputModel}
- * @return the object to be rendered
+ * @return the value to be rendered
*/
protected Object filterModel(Map Setting a custom-configured {@code ObjectMapper} is one way to take further control
- * of the JSON serialization process. For example, an extended {@link SerializerFactory}
- * can be configured that provides custom serializers for specific types. The other option
- * for refining the serialization process is to use Jackson's provided annotations on the
- * types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.
+ * Set the {@code ObjectMapper} for this view.
+ * If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} will be used.
+ * Setting a custom-configured {@code ObjectMapper} is one way to take further control of
+ * the JSON serialization process. The other option is to use Jackson's provided annotations
+ * on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.
*/
public void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "'objectMapper' must not be null");
@@ -101,14 +100,15 @@ public class MappingJacksonJsonView extends AbstractView {
configurePrettyPrint();
}
- private void configurePrettyPrint() {
- if (this.prettyPrint != null) {
- this.objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, this.prettyPrint);
- }
+ /**
+ * Return the {@code ObjectMapper} for this view.
+ */
+ public final ObjectMapper getObjectMapper() {
+ return this.objectMapper;
}
/**
- * Set the {@code JsonEncoding} for this converter.
+ * Set the {@code JsonEncoding} for this view.
* By default, {@linkplain JsonEncoding#UTF8 UTF-8} is used.
*/
public void setEncoding(JsonEncoding encoding) {
@@ -116,9 +116,16 @@ public class MappingJacksonJsonView extends AbstractView {
this.encoding = encoding;
}
+ /**
+ * Return the {@code JsonEncoding} for this view.
+ */
+ public final JsonEncoding getEncoding() {
+ return this.encoding;
+ }
+
/**
* Indicates whether the JSON output by this view should be prefixed with "{} && ".
- * Default is false.
+ * Default is {@code false}.
* Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
* 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
@@ -129,12 +136,11 @@ public class MappingJacksonJsonView extends AbstractView {
}
/**
- * Whether to use the {@link DefaultPrettyPrinter} when writing JSON.
+ * Whether to use the default pretty printer when writing JSON.
* This is a shortcut for setting up an {@code ObjectMapper} as follows:
* The default value is {@code false}.
*/
@@ -143,6 +149,12 @@ public class MappingJacksonJsonView extends AbstractView {
configurePrettyPrint();
}
+ private void configurePrettyPrint() {
+ if (this.prettyPrint != null) {
+ this.objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, this.prettyPrint);
+ }
+ }
+
/**
* Set the attribute in the model that should be rendered by this view.
* When set, all other model attributes will be ignored.
@@ -162,7 +174,7 @@ public class MappingJacksonJsonView extends AbstractView {
/**
* Return the attributes in the model that should be rendered by this view.
*/
- public Set The default implementation removes {@link BindingResult} instances and entries
* not included in the {@link #setRenderedAttributes renderedAttributes} property.
* @param model the model, as passed on to {@link #renderMergedOutputModel}
- * @return the object to be rendered
+ * @return the value to be rendered
*/
protected Object filterModel(Map
* ObjectMapper mapper = new ObjectMapper();
* mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
- * converter.setObjectMapper(mapper);
*
*