diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java
index c74ea2ba9dc..18407f9effc 100644
--- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java
+++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java
@@ -16,9 +16,11 @@
package org.springframework.web.servlet.view.json;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -26,7 +28,6 @@ import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerFactory;
-
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
@@ -38,8 +39,9 @@ import org.springframework.web.servlet.view.AbstractView;
* href="http://jackson.codehaus.org/">Jackson's {@link ObjectMapper}.
*
*
By default, the entire contents of the model map (with the exception of framework-specific classes) will be
- * encoded as JSON. For cases where the contents of the map need to be filtered, users may specify a specific set of
- * model attributes to encode via the {@link #setRenderedAttributes(Set) renderedAttributes} property.
+ * encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON alone via
+ * {@link #setExtractValueFromSingleKeyModel(boolean)}. Or you can select specific model attributes to be encoded
+ * as JSON via ... TODO
*
* @author Jeremy Grelle
* @author Arjen Poutsma
@@ -60,7 +62,7 @@ public class MappingJacksonJsonView extends AbstractView {
private boolean prefixJson = false;
- private Set renderedAttributes;
+ private Set modelKeys;
private boolean extractValueFromSingleKeyModel = false;
@@ -96,7 +98,7 @@ public class MappingJacksonJsonView extends AbstractView {
}
/**
- * Indicates whether the JSON output by this view should be prefixed with "{@code {} &&}". Default is false.
+ * Indicates whether the JSON output by this view should be prefixed with "{} && ". Default is 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,
@@ -105,20 +107,45 @@ public class MappingJacksonJsonView extends AbstractView {
public void setPrefixJson(boolean prefixJson) {
this.prefixJson = prefixJson;
}
+
+ /**
+ * Sets the attribute in the model that should be rendered by this view.
+ * When set, all other model attributes will be ignored.
+ */
+ public void setModelKey(String modelKey) {
+ this.modelKeys = Collections.singleton(modelKey);
+ }
+
+ /**
+ * Sets the attributes in the model that should be rendered by this view.
+ * When set, all other model attributes will be ignored.
+ */
+ public void setModelKeys(Set modelKeys) {
+ this.modelKeys = modelKeys;
+ }
/**
* Returns the attributes in the model that should be rendered by this view.
*/
- public Set getRenderedAttributes() {
- return renderedAttributes;
+ public Set getModelKeys() {
+ return modelKeys;
}
/**
- * Sets the attributes in the model that should be rendered by this view. When set, all other model attributes will be
- * ignored.
+ * Sets the attributes in the model that should be rendered by this view.
+ * When set, all other model attributes will be ignored.
+ * @deprecated use {@link #setModelKeys(Set)} instead
*/
public void setRenderedAttributes(Set renderedAttributes) {
- this.renderedAttributes = renderedAttributes;
+ this.modelKeys = renderedAttributes;
+ }
+
+ /**
+ * Returns the attributes in the model that should be rendered by this view.
+ * @deprecated use {@link #getModelKeys()} instead
+ */
+ public Set getRenderedAttributes() {
+ return modelKeys;
}
/**
@@ -180,7 +207,7 @@ public class MappingJacksonJsonView extends AbstractView {
protected Object filterModel(Map model) {
Map result = new HashMap(model.size());
Set renderedAttributes =
- !CollectionUtils.isEmpty(this.renderedAttributes) ? this.renderedAttributes : model.keySet();
+ !CollectionUtils.isEmpty(this.modelKeys) ? this.modelKeys : model.keySet();
for (Map.Entry entry : model.entrySet()) {
if (!(entry.getValue() instanceof BindingResult) && renderedAttributes.contains(entry.getKey())) {
result.put(entry.getKey(), entry.getValue());
diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTest.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTest.java
index 7762e05a0ff..710ec53dc95 100644
--- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTest.java
+++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTest.java
@@ -16,6 +16,13 @@
package org.springframework.web.servlet.view.json;
+import static org.easymock.EasyMock.createMock;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
@@ -31,14 +38,11 @@ import org.codehaus.jackson.map.SerializerFactory;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.ser.BeanSerializerFactory;
-import static org.easymock.EasyMock.*;
-import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ScriptableObject;
-
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.validation.BindingResult;
@@ -153,7 +157,8 @@ public class MappingJacksonJsonViewTest {
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
SerializerFactory factory = new DelegatingSerializerFactory();
- ObjectMapper mapper = new ObjectMapper(factory);
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.setSerializerFactory(factory);
view.setObjectMapper(mapper);
Object bean = new TestBeanSimple();
@@ -178,7 +183,7 @@ public class MappingJacksonJsonViewTest {
attrs.add("baz");
attrs.add("nil");
- view.setRenderedAttributes(attrs);
+ view.setModelKeys(attrs);
Map model = new HashMap();
model.put("foo", "foo");
model.put("bar", "bar");
@@ -207,6 +212,7 @@ public class MappingJacksonJsonViewTest {
assertSame(bean, actual);
}
+ @SuppressWarnings("rawtypes")
@Test
public void filterTwoKeyModel() throws Exception {
view.setExtractValueFromSingleKeyModel(true);