Browse Source
Jackson 2 uses completely new package names and new maven artifact ids.
This change adds Jackson 2 as an optional dependency and also provides
MappingJackson2HttpMessageConverter and MappingJackson2JsonView for use
with the new version.
The MVC namespace and the MVC Java config detect and use
MappingJackson2HttpMessageConverter if Jackson 2 is present.
Otherwise if Jackson 1.x is present,
then MappingJacksonHttpMessageConverter is used.
Issue: SPR-9302
Backport-Issue: SPR-9507
Backport-Commit: e63ca04fdb
3.1.x
20 changed files with 1051 additions and 161 deletions
@ -0,0 +1,220 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2012 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
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; |
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonEncoding; |
||||||
|
import com.fasterxml.jackson.core.JsonGenerator; |
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||||
|
|
||||||
|
import org.springframework.util.Assert; |
||||||
|
import org.springframework.util.CollectionUtils; |
||||||
|
import org.springframework.validation.BindingResult; |
||||||
|
import org.springframework.web.servlet.View; |
||||||
|
import org.springframework.web.servlet.view.AbstractView; |
||||||
|
|
||||||
|
/** |
||||||
|
* Spring MVC {@link View} that renders JSON content by serializing the model for the current request |
||||||
|
* using <a href="http://jackson.codehaus.org/">Jackson 2's</a> {@link ObjectMapper}. |
||||||
|
* |
||||||
|
* <p>By default, the entire contents of the model map (with the exception of framework-specific classes) |
||||||
|
* will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON |
||||||
|
* alone via {@link #setExtractValueFromSingleKeyModel}. |
||||||
|
* |
||||||
|
* @author Jeremy Grelle |
||||||
|
* @author Arjen Poutsma |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
* @since 3.2 |
||||||
|
* @see org.springframework.http.converter.json.MappingJackson2HttpMessageConverter |
||||||
|
*/ |
||||||
|
public class MappingJackson2JsonView extends AbstractView { |
||||||
|
|
||||||
|
/** |
||||||
|
* Default content type. Overridable as bean property. |
||||||
|
*/ |
||||||
|
public static final String DEFAULT_CONTENT_TYPE = "application/json"; |
||||||
|
|
||||||
|
|
||||||
|
private ObjectMapper objectMapper = new ObjectMapper(); |
||||||
|
|
||||||
|
private JsonEncoding encoding = JsonEncoding.UTF8; |
||||||
|
|
||||||
|
private boolean prefixJson = false; |
||||||
|
|
||||||
|
private Set<String> modelKeys; |
||||||
|
|
||||||
|
private boolean extractValueFromSingleKeyModel = false; |
||||||
|
|
||||||
|
private boolean disableCaching = true; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Construct a new {@code JacksonJsonView}, setting the content type to {@code application/json}. |
||||||
|
*/ |
||||||
|
public MappingJackson2JsonView() { |
||||||
|
setContentType(DEFAULT_CONTENT_TYPE); |
||||||
|
setExposePathVariables(false); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the {@code ObjectMapper} for this view. |
||||||
|
* If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used. |
||||||
|
* <p>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. |
||||||
|
*/ |
||||||
|
public void setObjectMapper(ObjectMapper objectMapper) { |
||||||
|
Assert.notNull(objectMapper, "'objectMapper' must not be null"); |
||||||
|
this.objectMapper = objectMapper; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Set the {@code JsonEncoding} for this converter. |
||||||
|
* By default, {@linkplain JsonEncoding#UTF8 UTF-8} is used. |
||||||
|
*/ |
||||||
|
public void setEncoding(JsonEncoding encoding) { |
||||||
|
Assert.notNull(encoding, "'encoding' must not be null"); |
||||||
|
this.encoding = encoding; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Indicates whether the JSON output by this view should be prefixed with <tt>"{} && "</tt>. |
||||||
|
* Default is false. |
||||||
|
* <p>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 |
||||||
|
* on the string, the prefix would need to be ignored. |
||||||
|
*/ |
||||||
|
public void setPrefixJson(boolean prefixJson) { |
||||||
|
this.prefixJson = prefixJson; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Set 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); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Set 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<String> modelKeys) { |
||||||
|
this.modelKeys = modelKeys; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Return the attributes in the model that should be rendered by this view. |
||||||
|
*/ |
||||||
|
public Set<String> getModelKeys() { |
||||||
|
return this.modelKeys; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Set 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 |
||||||
|
*/ |
||||||
|
@Deprecated |
||||||
|
public void setRenderedAttributes(Set<String> renderedAttributes) { |
||||||
|
this.modelKeys = renderedAttributes; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Return the attributes in the model that should be rendered by this view. |
||||||
|
* @deprecated use {@link #getModelKeys()} instead |
||||||
|
*/ |
||||||
|
@Deprecated |
||||||
|
public Set<String> getRenderedAttributes() { |
||||||
|
return this.modelKeys; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Set whether to serialize models containing a single attribute as a map or whether to |
||||||
|
* extract the single value from the model and serialize it directly. |
||||||
|
* <p>The effect of setting this flag is similar to using {@code MappingJacksonHttpMessageConverter} |
||||||
|
* with an {@code @ResponseBody} request-handling method. |
||||||
|
* <p>Default is {@code false}. |
||||||
|
*/ |
||||||
|
public void setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyModel) { |
||||||
|
this.extractValueFromSingleKeyModel = extractValueFromSingleKeyModel; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Disables caching of the generated JSON. |
||||||
|
* <p>Default is {@code true}, which will prevent the client from caching the generated JSON. |
||||||
|
*/ |
||||||
|
public void setDisableCaching(boolean disableCaching) { |
||||||
|
this.disableCaching = disableCaching; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) { |
||||||
|
response.setContentType(getContentType()); |
||||||
|
response.setCharacterEncoding(this.encoding.getJavaName()); |
||||||
|
if (this.disableCaching) { |
||||||
|
response.addHeader("Pragma", "no-cache"); |
||||||
|
response.addHeader("Cache-Control", "no-cache, no-store, max-age=0"); |
||||||
|
response.addDateHeader("Expires", 1L); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, |
||||||
|
HttpServletResponse response) throws Exception { |
||||||
|
|
||||||
|
Object value = filterModel(model); |
||||||
|
JsonGenerator generator = |
||||||
|
this.objectMapper.getJsonFactory().createJsonGenerator(response.getOutputStream(), this.encoding); |
||||||
|
if (this.prefixJson) { |
||||||
|
generator.writeRaw("{} && "); |
||||||
|
} |
||||||
|
this.objectMapper.writeValue(generator, value); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Filters out undesired attributes from the given model. |
||||||
|
* The return value can be either another {@link Map} or a single value object. |
||||||
|
* <p>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 |
||||||
|
*/ |
||||||
|
protected Object filterModel(Map<String, Object> model) { |
||||||
|
Map<String, Object> result = new HashMap<String, Object>(model.size()); |
||||||
|
Set<String> renderedAttributes = (!CollectionUtils.isEmpty(this.modelKeys) ? this.modelKeys : model.keySet()); |
||||||
|
for (Map.Entry<String, Object> entry : model.entrySet()) { |
||||||
|
if (!(entry.getValue() instanceof BindingResult) && renderedAttributes.contains(entry.getKey())) { |
||||||
|
result.put(entry.getKey(), entry.getValue()); |
||||||
|
} |
||||||
|
} |
||||||
|
return (this.extractValueFromSingleKeyModel && result.size() == 1 ? result.values().iterator().next() : result); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,352 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2012 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
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; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
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; |
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator; |
||||||
|
import com.fasterxml.jackson.databind.BeanProperty; |
||||||
|
import com.fasterxml.jackson.databind.JavaType; |
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException; |
||||||
|
import com.fasterxml.jackson.databind.JsonSerializer; |
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider; |
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
||||||
|
import com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig; |
||||||
|
import com.fasterxml.jackson.databind.ser.BasicSerializerFactory; |
||||||
|
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory; |
||||||
|
import com.fasterxml.jackson.databind.ser.SerializerFactory; |
||||||
|
import com.fasterxml.jackson.databind.ser.Serializers; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Jeremy Grelle |
||||||
|
* @author Arjen Poutsma |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
*/ |
||||||
|
public class MappingJackson2JsonViewTest { |
||||||
|
|
||||||
|
private MappingJackson2JsonView view; |
||||||
|
|
||||||
|
private MockHttpServletRequest request; |
||||||
|
|
||||||
|
private MockHttpServletResponse response; |
||||||
|
|
||||||
|
private Context jsContext; |
||||||
|
|
||||||
|
private ScriptableObject jsScope; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
request = new MockHttpServletRequest(); |
||||||
|
response = new MockHttpServletResponse(); |
||||||
|
|
||||||
|
jsContext = ContextFactory.getGlobal().enterContext(); |
||||||
|
jsScope = jsContext.initStandardObjects(); |
||||||
|
|
||||||
|
view = new MappingJackson2JsonView(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void isExposePathVars() { |
||||||
|
assertEquals("Must not expose path variables", false, view.isExposePathVariables()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void renderSimpleMap() throws Exception { |
||||||
|
|
||||||
|
Map<String, Object> model = new HashMap<String, Object>(); |
||||||
|
model.put("bindingResult", createMock("binding_result", BindingResult.class)); |
||||||
|
model.put("foo", "bar"); |
||||||
|
|
||||||
|
view.render(model, request, response); |
||||||
|
|
||||||
|
assertEquals("no-cache", response.getHeader("Pragma")); |
||||||
|
assertEquals("no-cache, no-store, max-age=0", response.getHeader("Cache-Control")); |
||||||
|
assertNotNull(response.getHeader("Expires")); |
||||||
|
|
||||||
|
assertEquals(MappingJacksonJsonView.DEFAULT_CONTENT_TYPE, response.getContentType()); |
||||||
|
|
||||||
|
String jsonResult = response.getContentAsString(); |
||||||
|
assertTrue(jsonResult.length() > 0); |
||||||
|
|
||||||
|
validateResult(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void renderCaching() throws Exception { |
||||||
|
view.setDisableCaching(false); |
||||||
|
|
||||||
|
Map<String, Object> model = new HashMap<String, Object>(); |
||||||
|
model.put("bindingResult", createMock("binding_result", BindingResult.class)); |
||||||
|
model.put("foo", "bar"); |
||||||
|
|
||||||
|
view.render(model, request, response); |
||||||
|
|
||||||
|
assertNull(response.getHeader("Pragma")); |
||||||
|
assertNull(response.getHeader("Cache-Control")); |
||||||
|
assertNull(response.getHeader("Expires")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void renderSimpleMapPrefixed() throws Exception { |
||||||
|
view.setPrefixJson(true); |
||||||
|
renderSimpleMap(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void renderSimpleBean() throws Exception { |
||||||
|
|
||||||
|
Object bean = new TestBeanSimple(); |
||||||
|
Map<String, Object> model = new HashMap<String, Object>(); |
||||||
|
model.put("bindingResult", createMock("binding_result", BindingResult.class)); |
||||||
|
model.put("foo", bean); |
||||||
|
|
||||||
|
view.render(model, request, response); |
||||||
|
|
||||||
|
assertTrue(response.getContentAsString().length() > 0); |
||||||
|
|
||||||
|
validateResult(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void renderSimpleBeanPrefixed() throws Exception { |
||||||
|
|
||||||
|
view.setPrefixJson(true); |
||||||
|
renderSimpleBean(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void renderWithCustomSerializerLocatedByAnnotation() throws Exception { |
||||||
|
|
||||||
|
Object bean = new TestBeanSimpleAnnotated(); |
||||||
|
Map<String, Object> model = new HashMap<String, Object>(); |
||||||
|
model.put("foo", bean); |
||||||
|
|
||||||
|
view.render(model, request, response); |
||||||
|
|
||||||
|
assertTrue(response.getContentAsString().length() > 0); |
||||||
|
assertEquals("{\"foo\":{\"testBeanSimple\":\"custom\"}}", response.getContentAsString()); |
||||||
|
|
||||||
|
validateResult(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void renderWithCustomSerializerLocatedByFactory() throws Exception { |
||||||
|
|
||||||
|
SerializerFactory factory = new DelegatingSerializerFactory(null); |
||||||
|
ObjectMapper mapper = new ObjectMapper(); |
||||||
|
mapper.setSerializerFactory(factory); |
||||||
|
view.setObjectMapper(mapper); |
||||||
|
|
||||||
|
Object bean = new TestBeanSimple(); |
||||||
|
Map<String, Object> model = new HashMap<String, Object>(); |
||||||
|
model.put("foo", bean); |
||||||
|
model.put("bar", new TestChildBean()); |
||||||
|
|
||||||
|
view.render(model, request, response); |
||||||
|
|
||||||
|
String result = response.getContentAsString(); |
||||||
|
assertTrue(result.length() > 0); |
||||||
|
assertTrue(result.contains("\"foo\":{\"testBeanSimple\":\"custom\"}")); |
||||||
|
|
||||||
|
validateResult(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void renderOnlyIncludedAttributes() throws Exception { |
||||||
|
|
||||||
|
Set<String> attrs = new HashSet<String>(); |
||||||
|
attrs.add("foo"); |
||||||
|
attrs.add("baz"); |
||||||
|
attrs.add("nil"); |
||||||
|
|
||||||
|
view.setModelKeys(attrs); |
||||||
|
Map<String, Object> model = new HashMap<String, Object>(); |
||||||
|
model.put("foo", "foo"); |
||||||
|
model.put("bar", "bar"); |
||||||
|
model.put("baz", "baz"); |
||||||
|
|
||||||
|
view.render(model, request, response); |
||||||
|
|
||||||
|
String result = response.getContentAsString(); |
||||||
|
assertTrue(result.length() > 0); |
||||||
|
assertTrue(result.contains("\"foo\":\"foo\"")); |
||||||
|
assertTrue(result.contains("\"baz\":\"baz\"")); |
||||||
|
|
||||||
|
validateResult(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void filterSingleKeyModel() throws Exception { |
||||||
|
view.setExtractValueFromSingleKeyModel(true); |
||||||
|
|
||||||
|
Map<String, Object> model = new HashMap<String, Object>(); |
||||||
|
TestBeanSimple bean = new TestBeanSimple(); |
||||||
|
model.put("foo", bean); |
||||||
|
|
||||||
|
Object actual = view.filterModel(model); |
||||||
|
|
||||||
|
assertSame(bean, actual); |
||||||
|
} |
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes") |
||||||
|
@Test |
||||||
|
public void filterTwoKeyModel() throws Exception { |
||||||
|
view.setExtractValueFromSingleKeyModel(true); |
||||||
|
|
||||||
|
Map<String, Object> model = new HashMap<String, Object>(); |
||||||
|
TestBeanSimple bean1 = new TestBeanSimple(); |
||||||
|
TestBeanSimple bean2 = new TestBeanSimple(); |
||||||
|
model.put("foo1", bean1); |
||||||
|
model.put("foo2", bean2); |
||||||
|
|
||||||
|
Object actual = view.filterModel(model); |
||||||
|
|
||||||
|
assertTrue(actual instanceof Map); |
||||||
|
assertSame(bean1, ((Map) actual).get("foo1")); |
||||||
|
assertSame(bean2, ((Map) actual).get("foo2")); |
||||||
|
} |
||||||
|
|
||||||
|
private void validateResult() throws Exception { |
||||||
|
Object jsResult = |
||||||
|
jsContext.evaluateString(jsScope, "(" + response.getContentAsString() + ")", "JSON Stream", 1, null); |
||||||
|
assertNotNull("Json Result did not eval as valid JavaScript", jsResult); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static class TestBeanSimple { |
||||||
|
|
||||||
|
private String value = "foo"; |
||||||
|
|
||||||
|
private boolean test = false; |
||||||
|
|
||||||
|
private long number = 42; |
||||||
|
|
||||||
|
private TestChildBean child = new TestChildBean(); |
||||||
|
|
||||||
|
public String getValue() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean getTest() { |
||||||
|
return test; |
||||||
|
} |
||||||
|
|
||||||
|
public long getNumber() { |
||||||
|
return number; |
||||||
|
} |
||||||
|
|
||||||
|
public Date getNow() { |
||||||
|
return new Date(); |
||||||
|
} |
||||||
|
|
||||||
|
public TestChildBean getChild() { |
||||||
|
return child; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@JsonSerialize(using=TestBeanSimpleSerializer.class) |
||||||
|
public static class TestBeanSimpleAnnotated extends TestBeanSimple { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public static class TestChildBean { |
||||||
|
|
||||||
|
private String value = "bar"; |
||||||
|
|
||||||
|
private String baz = null; |
||||||
|
|
||||||
|
private TestBeanSimple parent = null; |
||||||
|
|
||||||
|
public String getValue() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
public String getBaz() { |
||||||
|
return baz; |
||||||
|
} |
||||||
|
|
||||||
|
public TestBeanSimple getParent() { |
||||||
|
return parent; |
||||||
|
} |
||||||
|
|
||||||
|
public void setParent(TestBeanSimple parent) { |
||||||
|
this.parent = parent; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static class TestBeanSimpleSerializer extends JsonSerializer<Object> { |
||||||
|
|
||||||
|
@Override |
||||||
|
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException { |
||||||
|
jgen.writeStartObject(); |
||||||
|
jgen.writeFieldName("testBeanSimple"); |
||||||
|
jgen.writeString("custom"); |
||||||
|
jgen.writeEndObject(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static class DelegatingSerializerFactory extends BasicSerializerFactory { |
||||||
|
|
||||||
|
private SerializerFactory beanSerializer = BeanSerializerFactory.instance; |
||||||
|
|
||||||
|
protected DelegatingSerializerFactory(SerializerFactoryConfig config) { |
||||||
|
super(config); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public JsonSerializer<Object> createSerializer(SerializerProvider prov, JavaType type, BeanProperty property) throws JsonMappingException { |
||||||
|
if (type.getRawClass() == TestBeanSimple.class) { |
||||||
|
return new TestBeanSimpleSerializer(); |
||||||
|
} |
||||||
|
else { |
||||||
|
return beanSerializer.createSerializer(prov, type, property); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public SerializerFactory withConfig(SerializerFactoryConfig config) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected Iterable<Serializers> customSerializers() { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,188 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2012 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.http.converter.json; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.nio.charset.Charset; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.springframework.http.HttpInputMessage; |
||||||
|
import org.springframework.http.HttpOutputMessage; |
||||||
|
import org.springframework.http.MediaType; |
||||||
|
import org.springframework.http.converter.AbstractHttpMessageConverter; |
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException; |
||||||
|
import org.springframework.http.converter.HttpMessageNotWritableException; |
||||||
|
import org.springframework.util.Assert; |
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonEncoding; |
||||||
|
import com.fasterxml.jackson.core.JsonGenerator; |
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException; |
||||||
|
import com.fasterxml.jackson.databind.JavaType; |
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||||
|
|
||||||
|
/** |
||||||
|
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} |
||||||
|
* that can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson 2's</a> {@link ObjectMapper}. |
||||||
|
* |
||||||
|
* <p>This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances. |
||||||
|
* |
||||||
|
* <p>By default, this converter supports {@code application/json}. This can be overridden by setting the |
||||||
|
* {@link #setSupportedMediaTypes(List) supportedMediaTypes} property. |
||||||
|
* |
||||||
|
* @author Arjen Poutsma |
||||||
|
* @author Keith Donald |
||||||
|
* @since 3.2 |
||||||
|
* @see org.springframework.web.servlet.view.json.MappingJackson2JsonView |
||||||
|
*/ |
||||||
|
public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConverter<Object> { |
||||||
|
|
||||||
|
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); |
||||||
|
|
||||||
|
|
||||||
|
private ObjectMapper objectMapper = new ObjectMapper(); |
||||||
|
|
||||||
|
private boolean prefixJson = false; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Construct a new {@code BindingJacksonHttpMessageConverter}. |
||||||
|
*/ |
||||||
|
public MappingJackson2HttpMessageConverter() { |
||||||
|
super(new MediaType("application", "json", DEFAULT_CHARSET)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Set the {@code ObjectMapper} for this view. If not set, a default |
||||||
|
* {@link ObjectMapper#ObjectMapper() ObjectMapper} is used. |
||||||
|
* <p>Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON |
||||||
|
* serialization process. For example, an extended {@link org.codehaus.jackson.map.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. |
||||||
|
*/ |
||||||
|
public void setObjectMapper(ObjectMapper objectMapper) { |
||||||
|
Assert.notNull(objectMapper, "ObjectMapper must not be null"); |
||||||
|
this.objectMapper = objectMapper; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Return the underlying {@code ObjectMapper} for this view. |
||||||
|
*/ |
||||||
|
public ObjectMapper getObjectMapper() { |
||||||
|
return this.objectMapper; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false. |
||||||
|
* <p>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 on the |
||||||
|
* string, the prefix would need to be ignored. |
||||||
|
*/ |
||||||
|
public void setPrefixJson(boolean prefixJson) { |
||||||
|
this.prefixJson = prefixJson; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean canRead(Class<?> clazz, MediaType mediaType) { |
||||||
|
JavaType javaType = getJavaType(clazz); |
||||||
|
return (this.objectMapper.canDeserialize(javaType) && canRead(mediaType)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean canWrite(Class<?> clazz, MediaType mediaType) { |
||||||
|
return (this.objectMapper.canSerialize(clazz) && canWrite(mediaType)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected boolean supports(Class<?> clazz) { |
||||||
|
// should not be called, since we override canRead/Write instead
|
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) |
||||||
|
throws IOException, HttpMessageNotReadableException { |
||||||
|
|
||||||
|
JavaType javaType = getJavaType(clazz); |
||||||
|
try { |
||||||
|
return this.objectMapper.readValue(inputMessage.getBody(), javaType); |
||||||
|
} |
||||||
|
catch (JsonProcessingException ex) { |
||||||
|
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void writeInternal(Object object, HttpOutputMessage outputMessage) |
||||||
|
throws IOException, HttpMessageNotWritableException { |
||||||
|
|
||||||
|
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType()); |
||||||
|
JsonGenerator jsonGenerator = |
||||||
|
this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding); |
||||||
|
try { |
||||||
|
if (this.prefixJson) { |
||||||
|
jsonGenerator.writeRaw("{} && "); |
||||||
|
} |
||||||
|
this.objectMapper.writeValue(jsonGenerator, object); |
||||||
|
} |
||||||
|
catch (JsonProcessingException ex) { |
||||||
|
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Return the Jackson {@link JavaType} for the specified class. |
||||||
|
* <p>The default implementation returns {@link ObjectMapper#constructType(java.lang.reflect.Type)}, |
||||||
|
* but this can be overridden in subclasses, to allow for custom generic collection handling. |
||||||
|
* For instance: |
||||||
|
* <pre class="code"> |
||||||
|
* protected JavaType getJavaType(Class<?> clazz) { |
||||||
|
* if (List.class.isAssignableFrom(clazz)) { |
||||||
|
* return objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, MyBean.class); |
||||||
|
* } else { |
||||||
|
* return super.getJavaType(clazz); |
||||||
|
* } |
||||||
|
* } |
||||||
|
* </pre> |
||||||
|
* @param clazz the class to return the java type for |
||||||
|
* @return the java type |
||||||
|
*/ |
||||||
|
protected JavaType getJavaType(Class<?> clazz) { |
||||||
|
return objectMapper.constructType(clazz); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Determine the JSON encoding to use for the given content type. |
||||||
|
* @param contentType the media type as requested by the caller |
||||||
|
* @return the JSON encoding to use (never <code>null</code>) |
||||||
|
*/ |
||||||
|
protected JsonEncoding getJsonEncoding(MediaType contentType) { |
||||||
|
if (contentType != null && contentType.getCharSet() != null) { |
||||||
|
Charset charset = contentType.getCharSet(); |
||||||
|
for (JsonEncoding encoding : JsonEncoding.values()) { |
||||||
|
if (charset.name().equals(encoding.getJavaName())) { |
||||||
|
return encoding; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return JsonEncoding.UTF8; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue