Browse Source

Upgraded to Jackson 1.9 (raising minimum to 1.8+) and 2.2

pull/292/head
Juergen Hoeller 12 years ago
parent
commit
767bd3f3f8
  1. 60
      build.gradle
  2. 18
      spring-jms/src/test/java/org/springframework/jms/support/converter/MappingJackson2MessageConverterTests.java
  3. 31
      spring-jms/src/test/java/org/springframework/jms/support/converter/MappingJacksonMessageConverterTests.java
  4. 60
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java
  5. 45
      spring-web/src/main/java/org/springframework/http/converter/json/JacksonObjectMapperFactoryBean.java
  6. 13
      spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java
  7. 17
      spring-web/src/main/java/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java
  8. 4
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java
  9. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java
  10. 56
      spring-webmvc/src/test/java/org/springframework/web/servlet/view/json/MappingJackson2JsonViewTests.java
  11. 40
      spring-webmvc/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTests.java

60
build.gradle

@ -82,7 +82,6 @@ configure(allprojects) { project -> @@ -82,7 +82,6 @@ configure(allprojects) { project ->
"http://docs.oracle.com/javase/6/docs/api",
"http://docs.oracle.com/javaee/6/api",
"http://portals.apache.org/pluto/portlet-2.0-apidocs/",
"http://commons.apache.org/proper/commons-lang/javadocs/api-release/",
"http://commons.apache.org/proper/commons-codec/apidocs/",
"http://docs.jboss.org/jbossas/javadoc/4.0.5/connector/",
"http://docs.jboss.org/jbossas/javadoc/7.1.2.Final/",
@ -91,8 +90,8 @@ configure(allprojects) { project -> @@ -91,8 +90,8 @@ configure(allprojects) { project ->
"http://docs.oracle.com/cd/E13222_01/wls/docs90/javadocs/", // commonj
"http://quartz-scheduler.org/api/2.1.5/",
"http://www.eclipse.org/aspectj/doc/released/aspectj5rt-api/",
"http://fasterxml.github.com/jackson-core/javadoc/2.0.0/",
"http://jackson.codehaus.org/1.4.2/javadoc/",
"http://fasterxml.github.com/jackson-core/javadoc/2.2.0/",
"http://jackson.codehaus.org/1.9.12/javadoc/",
"http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/topic/com.ibm.websphere.javadoc.doc/web/apidocs/",
"http://tiles.apache.org/framework/apidocs/",
"http://commons.apache.org/proper/commons-dbcp/apidocs/",
@ -292,8 +291,8 @@ project("spring-context") { @@ -292,8 +291,8 @@ project("spring-context") {
optional("org.apache.geronimo.specs:geronimo-jms_1.1_spec:1.1")
optional("org.eclipse.persistence:javax.persistence:2.0.0")
optional("org.beanshell:bsh:2.0b4")
optional("org.codehaus.groovy:groovy-all:1.8.8")
optional("org.jruby:jruby:1.6.5.1")
optional("org.codehaus.groovy:groovy-all:1.8.9")
optional("org.jruby:jruby:1.7.2")
optional("joda-time:joda-time:2.2")
optional("org.slf4j:slf4j-api:${slf4jVersion}")
optional("javax.validation:validation-api:1.0.0.GA")
@ -320,7 +319,7 @@ project("spring-tx") { @@ -320,7 +319,7 @@ project("spring-tx") {
compile("aopalliance:aopalliance:1.0")
provided("com.ibm.websphere:uow:6.0.2.17")
optional("javax.resource:connector-api:1.5")
optional("org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.0.0.Alpha1")
optional("javax.transaction:javax.transaction-api:1.2-b03")
optional("javax.ejb:ejb-api:3.0")
testCompile("org.eclipse.persistence:javax.persistence:2.0.0")
testCompile("org.aspectj:aspectjweaver:${aspectjVersion}")
@ -342,10 +341,9 @@ project("spring-oxm") { @@ -342,10 +341,9 @@ project("spring-oxm") {
compile(project(":spring-beans"))
compile(project(":spring-core"))
optional(project(":spring-context")) // for Jaxb2Marshaller
compile("commons-lang:commons-lang:2.5")
optional("com.thoughtworks.xstream:xstream:1.3.1")
optional("org.jibx:jibx-run:1.2.3")
optional("org.apache.xmlbeans:xmlbeans:2.4.0")
optional("com.thoughtworks.xstream:xstream:1.4.4")
optional("org.jibx:jibx-run:1.2.5")
optional("org.apache.xmlbeans:xmlbeans:2.6.0")
optional("org.codehaus.castor:castor-xml:1.3.2")
testCompile("org.codehaus.jettison:jettison:1.0.1")
testCompile("xmlunit:xmlunit:1.3")
@ -370,8 +368,8 @@ project("spring-jms") { @@ -370,8 +368,8 @@ project("spring-jms") {
provided("org.apache.geronimo.specs:geronimo-jms_1.1_spec:1.1")
optional("org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1")
optional("javax.resource:connector-api:1.5")
optional("org.codehaus.jackson:jackson-mapper-asl:1.4.2")
optional("com.fasterxml.jackson.core:jackson-databind:2.0.1")
optional("org.codehaus.jackson:jackson-mapper-asl:1.9.12")
optional("com.fasterxml.jackson.core:jackson-databind:2.2.0")
}
}
@ -401,17 +399,17 @@ project("spring-context-support") { @@ -401,17 +399,17 @@ project("spring-context-support") {
compile(project(":spring-context"))
optional(project(":spring-jdbc")) // for Quartz support
optional(project(":spring-tx")) // for Quartz support
optional("javax.mail:mail:1.4")
optional("javax.cache:cache-api:0.5")
optional("net.sf.ehcache:ehcache-core:2.6.3")
optional("org.quartz-scheduler:quartz:1.8.3") {
optional("javax.mail:mail:1.4.7")
optional("javax.cache:cache-api:0.6")
optional("net.sf.ehcache:ehcache-core:2.6.5")
optional("org.quartz-scheduler:quartz:1.8.6") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
optional("org.codehaus.fabric3.api:commonj:1.1.0")
optional("org.apache.velocity:velocity:1.7")
optional("org.freemarker:freemarker:2.3.15")
optional("org.freemarker:freemarker:2.3.19")
optional("com.lowagie:itext:2.1.7")
optional("net.sf.jasperreports:jasperreports:5.0.4")
optional("net.sf.jasperreports:jasperreports:5.1.0")
optional("org.slf4j:slf4j-api:${slf4jVersion}")
provided("javax.activation:activation:1.1")
testCompile("org.apache.poi:poi:3.9")
@ -443,8 +441,8 @@ project("spring-web") { @@ -443,8 +441,8 @@ project("spring-web") {
optional("rome:rome:1.0")
optional("commons-fileupload:commons-fileupload:1.3")
optional("org.apache.httpcomponents:httpclient:4.2")
optional("org.codehaus.jackson:jackson-mapper-asl:1.4.2")
optional("com.fasterxml.jackson.core:jackson-databind:2.0.1")
optional("org.codehaus.jackson:jackson-mapper-asl:1.9.12")
optional("com.fasterxml.jackson.core:jackson-databind:2.2.0")
optional("taglibs:standard:1.1.2")
optional("org.eclipse.jetty:jetty-servlet:8.1.5.v20120716") {
exclude group: "org.eclipse.jetty.orbit", module: "javax.servlet"
@ -494,8 +492,8 @@ project("spring-orm-hibernate4") { @@ -494,8 +492,8 @@ project("spring-orm-hibernate4") {
dependencies {
provided(project(":spring-tx"))
provided(project(":spring-jdbc"))
optional("org.hibernate:hibernate-core:4.2.0.Final")
optional("org.hibernate:hibernate-entitymanager:4.2.0.Final")
optional("org.hibernate:hibernate-core:4.2.1.Final")
optional("org.hibernate:hibernate-entitymanager:4.2.1.Final")
optional(project(":spring-web"))
optional("javax.servlet:javax.servlet-api:3.0.1")
}
@ -519,15 +517,15 @@ project("spring-webmvc") { @@ -519,15 +517,15 @@ project("spring-webmvc") {
optional("net.sourceforge.jexcelapi:jxl:2.6.3")
optional("org.apache.poi:poi:3.9")
optional("com.lowagie:itext:2.1.7")
optional("net.sf.jasperreports:jasperreports:5.0.4") {
optional("net.sf.jasperreports:jasperreports:5.1.0") {
exclude group: "xml-apis", module: "xml-apis"
}
optional("rome:rome:1.0")
optional("org.apache.velocity:velocity:1.7")
optional("velocity-tools:velocity-tools-view:1.4")
optional("org.freemarker:freemarker:2.3.15")
optional("org.codehaus.jackson:jackson-mapper-asl:1.4.2")
optional("com.fasterxml.jackson.core:jackson-databind:2.0.1")
optional("org.freemarker:freemarker:2.3.19")
optional("org.codehaus.jackson:jackson-mapper-asl:1.9.12")
optional("com.fasterxml.jackson.core:jackson-databind:2.2.0")
provided("javax.servlet:jstl:1.2")
provided("javax.servlet:javax.servlet-api:3.0.1")
provided("javax.servlet.jsp:jsp-api:2.1")
@ -672,14 +670,14 @@ project("spring-test-mvc") { @@ -672,14 +670,14 @@ project("spring-test-mvc") {
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
testCompile("javax.servlet:jstl:1.2")
testCompile("org.hibernate:hibernate-validator:4.3.0.Final")
testCompile("org.codehaus.jackson:jackson-mapper-asl:1.4.2")
testCompile("com.fasterxml.jackson.core:jackson-databind:2.0.1")
testCompile("org.codehaus.jackson:jackson-mapper-asl:1.9.12")
testCompile("com.fasterxml.jackson.core:jackson-databind:2.2.0")
testCompile(project(":spring-context-support"))
testCompile(project(":spring-oxm"))
testCompile("com.thoughtworks.xstream:xstream:1.3.1")
testCompile("rome:rome:1.0")
testCompile("javax.activation:activation:1.1")
testCompile("javax.mail:mail:1.4")
testCompile("javax.mail:mail:1.4.7")
testCompile("org.apache.tiles:tiles-request-api:1.0.1")
testCompile("org.apache.tiles:tiles-api:3.0.1")
testCompile("org.apache.tiles:tiles-core:3.0.1") {
@ -704,7 +702,7 @@ project("spring-aspects") { @@ -704,7 +702,7 @@ project("spring-aspects") {
optional(project(":spring-orm")) // for JPA exception translation support
aspects(project(":spring-orm"))
provided("org.eclipse.persistence:javax.persistence:2.0.0")
testCompile("javax.mail:mail:1.4")
testCompile("javax.mail:mail:1.4.7")
ajc("org.aspectj:aspectjtools:${aspectjVersion}")
rt("org.aspectj:aspectjrt:${aspectjVersion}")
compile("org.aspectj:aspectjweaver:${aspectjVersion}")
@ -754,7 +752,7 @@ configure(rootProject) { @@ -754,7 +752,7 @@ configure(rootProject) {
testCompile(project(":spring-web"))
testCompile(project(":spring-webmvc-portlet"))
testCompile(project(":spring-orm"))
testCompile("org.hibernate:hibernate-core:4.2.0.Final")
testCompile("org.hibernate:hibernate-core:4.2.1.Final")
testCompile("javax.servlet:javax.servlet-api:3.0.1")
testCompile("javax.portlet:portlet-api:2.0")
testCompile("javax.inject:javax.inject:1")

18
spring-jms/src/test/java/org/springframework/jms/support/converter/MappingJackson2MessageConverterTests.java

@ -44,6 +44,7 @@ public class MappingJackson2MessageConverterTests { @@ -44,6 +44,7 @@ public class MappingJackson2MessageConverterTests {
private Session sessionMock;
@Before
public void setUp() throws Exception {
sessionMock = mock(Session.class);
@ -52,6 +53,7 @@ public class MappingJackson2MessageConverterTests { @@ -52,6 +53,7 @@ public class MappingJackson2MessageConverterTests {
converter.setTypeIdPropertyName("__typeid__");
}
@Test
public void toBytesMessage() throws Exception {
BytesMessage bytesMessageMock = mock(BytesMessage.class);
@ -74,8 +76,7 @@ public class MappingJackson2MessageConverterTests { @@ -74,8 +76,7 @@ public class MappingJackson2MessageConverterTests {
byte[] bytes = "{\"foo\":\"bar\"}".getBytes();
final ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
given(bytesMessageMock.getStringProperty("__typeid__")).willReturn(
Object.class.getName());
given(bytesMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName());
given(bytesMessageMock.propertyExists("__encoding__")).willReturn(false);
given(bytesMessageMock.getBodyLength()).willReturn(new Long(bytes.length));
given(bytesMessageMock.readBytes(any(byte[].class))).willAnswer(
@ -96,8 +97,7 @@ public class MappingJackson2MessageConverterTests { @@ -96,8 +97,7 @@ public class MappingJackson2MessageConverterTests {
TextMessage textMessageMock = mock(TextMessage.class);
Date toBeMarshalled = new Date();
given(sessionMock.createTextMessage(isA(String.class))).willReturn(
textMessageMock);
given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
converter.toMessage(toBeMarshalled, sessionMock);
verify(textMessageMock).setStringProperty("__typeid__", Date.class.getName());
@ -110,8 +110,7 @@ public class MappingJackson2MessageConverterTests { @@ -110,8 +110,7 @@ public class MappingJackson2MessageConverterTests {
Map<String, String> toBeMarshalled = new HashMap<String, String>();
toBeMarshalled.put("foo", "bar");
given(sessionMock.createTextMessage(isA(String.class))).willReturn(
textMessageMock);
given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
converter.toMessage(toBeMarshalled, sessionMock);
verify(textMessageMock).setStringProperty("__typeid__", HashMap.class.getName());
@ -123,8 +122,7 @@ public class MappingJackson2MessageConverterTests { @@ -123,8 +122,7 @@ public class MappingJackson2MessageConverterTests {
Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar");
String text = "{\"foo\":\"bar\"}";
given(textMessageMock.getStringProperty("__typeid__")).willReturn(
Object.class.getName());
given(textMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName());
given(textMessageMock.getText()).willReturn(text);
Object result = converter.fromMessage(textMessageMock);
@ -137,11 +135,11 @@ public class MappingJackson2MessageConverterTests { @@ -137,11 +135,11 @@ public class MappingJackson2MessageConverterTests {
Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar");
String text = "{\"foo\":\"bar\"}";
given(textMessageMock.getStringProperty("__typeid__")).willReturn(
HashMap.class.getName());
given(textMessageMock.getStringProperty("__typeid__")).willReturn(HashMap.class.getName());
given(textMessageMock.getText()).willReturn(text);
Object result = converter.fromMessage(textMessageMock);
assertEquals("Invalid result", result, unmarshalled);
}
}

31
spring-jms/src/test/java/org/springframework/jms/support/converter/MappingJacksonMessageConverterTests.java

@ -18,6 +18,7 @@ package org.springframework.jms.support.converter; @@ -18,6 +18,7 @@ package org.springframework.jms.support.converter;
import java.io.ByteArrayInputStream;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@ -43,6 +44,7 @@ public class MappingJacksonMessageConverterTests { @@ -43,6 +44,7 @@ public class MappingJacksonMessageConverterTests {
private Session sessionMock;
@Before
public void setUp() throws Exception {
sessionMock = mock(Session.class);
@ -51,22 +53,22 @@ public class MappingJacksonMessageConverterTests { @@ -51,22 +53,22 @@ public class MappingJacksonMessageConverterTests {
converter.setTypeIdPropertyName("__typeid__");
}
@Test
public void toBytesMessage() throws Exception {
BytesMessage bytesMessageMock = mock(BytesMessage.class);
Object toBeMarshalled = new Object();
Date toBeMarshalled = new Date();
given(sessionMock.createBytesMessage()).willReturn(bytesMessageMock);
converter.toMessage(toBeMarshalled, sessionMock);
verify(bytesMessageMock).setStringProperty("__encoding__", "UTF-8");
verify(bytesMessageMock).setStringProperty("__typeid__", Object.class.getName());
verify(bytesMessageMock).setStringProperty("__typeid__", Date.class.getName());
verify(bytesMessageMock).writeBytes(isA(byte[].class));
}
@Test
@SuppressWarnings("serial")
public void fromBytesMessage() throws Exception {
BytesMessage bytesMessageMock = mock(BytesMessage.class);
Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar");
@ -74,13 +76,11 @@ public class MappingJacksonMessageConverterTests { @@ -74,13 +76,11 @@ public class MappingJacksonMessageConverterTests {
byte[] bytes = "{\"foo\":\"bar\"}".getBytes();
final ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
given(bytesMessageMock.getStringProperty("__typeid__")).willReturn(
Object.class.getName());
given(bytesMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName());
given(bytesMessageMock.propertyExists("__encoding__")).willReturn(false);
given(bytesMessageMock.getBodyLength()).willReturn(new Long(bytes.length));
given(bytesMessageMock.readBytes(any(byte[].class))).willAnswer(
new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return byteStream.read((byte[]) invocation.getArguments()[0]);
@ -95,13 +95,12 @@ public class MappingJacksonMessageConverterTests { @@ -95,13 +95,12 @@ public class MappingJacksonMessageConverterTests {
public void toTextMessageWithObject() throws Exception {
converter.setTargetType(MessageType.TEXT);
TextMessage textMessageMock = mock(TextMessage.class);
Object toBeMarshalled = new Object();
Date toBeMarshalled = new Date();
given(sessionMock.createTextMessage(isA(String.class))).willReturn(
textMessageMock);
given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
converter.toMessage(toBeMarshalled, sessionMock);
verify(textMessageMock).setStringProperty("__typeid__", Object.class.getName());
verify(textMessageMock).setStringProperty("__typeid__", Date.class.getName());
}
@Test
@ -110,11 +109,10 @@ public class MappingJacksonMessageConverterTests { @@ -110,11 +109,10 @@ public class MappingJacksonMessageConverterTests {
TextMessage textMessageMock = mock(TextMessage.class);
Map<String, String> toBeMarshalled = new HashMap<String, String>();
toBeMarshalled.put("foo", "bar");
given(sessionMock.createTextMessage(isA(String.class))).willReturn(
textMessageMock);
converter.toMessage(toBeMarshalled, sessionMock);
given(sessionMock.createTextMessage(isA(String.class))).willReturn(textMessageMock);
converter.toMessage(toBeMarshalled, sessionMock);
verify(textMessageMock).setStringProperty("__typeid__", HashMap.class.getName());
}
@ -124,8 +122,7 @@ public class MappingJacksonMessageConverterTests { @@ -124,8 +122,7 @@ public class MappingJacksonMessageConverterTests {
Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar");
String text = "{\"foo\":\"bar\"}";
given(textMessageMock.getStringProperty("__typeid__")).willReturn(
Object.class.getName());
given(textMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName());
given(textMessageMock.getText()).willReturn(text);
Object result = converter.fromMessage(textMessageMock);
@ -138,11 +135,11 @@ public class MappingJacksonMessageConverterTests { @@ -138,11 +135,11 @@ public class MappingJacksonMessageConverterTests {
Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar");
String text = "{\"foo\":\"bar\"}";
given(textMessageMock.getStringProperty("__typeid__")).willReturn(
HashMap.class.getName());
given(textMessageMock.getStringProperty("__typeid__")).willReturn(HashMap.class.getName());
given(textMessageMock.getText()).willReturn(text);
Object result = converter.fromMessage(textMessageMock);
assertEquals("Invalid result", result, unmarshalled);
}
}

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

@ -1,5 +1,5 @@ @@ -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.
@ -22,11 +22,6 @@ import java.util.HashMap; @@ -22,11 +22,6 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
@ -38,9 +33,14 @@ import com.fasterxml.jackson.databind.ObjectMapper; @@ -38,9 +33,14 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* A FactoryBean for creating a Jackson {@link ObjectMapper} with setters to
* enable or disable Jackson features from within XML configuration.
* A {@link FactoryBean} for creating a Jackson 2.x {@link ObjectMapper} with setters
* to enable or disable Jackson features from within XML configuration.
*
* <p>Example usage with
* {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter}:
@ -56,7 +56,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule; @@ -56,7 +56,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
* &lt;/bean>
* </pre>
*
* <p>Example usage with {@link org.springframework.web.servlet.view.json.MappingJackson2JsonView}:
* <p>Example usage with MappingJackson2JsonView:
*
* <pre>
* &lt;bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
@ -94,12 +94,8 @@ import com.fasterxml.jackson.databind.module.SimpleModule; @@ -94,12 +94,8 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
* &lt;/bean>
* </pre>
*
* <p>Note: This BeanFctory is singleton, so if you need more than one you'll need
* to configure multiple instances.
*
* @author <a href="mailto:dmitry.katsubo@gmail.com">Dmitry Katsubo</a>
* @author Rossen Stoyanchev
*
* @since 3.2
*/
public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper>, InitializingBean {
@ -197,8 +193,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -197,8 +193,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
* {@link MapperFeature#AUTO_DETECT_GETTERS} option.
*/
public void setAutoDetectGettersSetters(boolean autoDetectGettersSetters) {
this.features.put(MapperFeature.AUTO_DETECT_SETTERS, autoDetectGettersSetters);
this.features.put(MapperFeature.AUTO_DETECT_GETTERS, autoDetectGettersSetters);
this.features.put(MapperFeature.AUTO_DETECT_SETTERS, autoDetectGettersSetters);
}
/**
@ -217,12 +213,9 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -217,12 +213,9 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
/**
* Specify features to enable.
*
* @see MapperFeature
* @see SerializationFeature
* @see DeserializationFeature
* @see org.codehaus.jackson.map.JsonParser.Feature
* @see org.codehaus.jackson.map.JsonGenerator.Feature
*/
public void setFeaturesToEnable(Object... featuresToEnable) {
if (featuresToEnable != null) {
@ -234,12 +227,11 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -234,12 +227,11 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
/**
* Specify features to disable.
*
* @see MapperFeature
* @see SerializationFeature
* @see DeserializationFeature
* @see org.codehaus.jackson.map.JsonParser.Feature
* @see org.codehaus.jackson.map.JsonGenerator.Feature
* @see com.fasterxml.jackson.core.JsonParser.Feature
* @see com.fasterxml.jackson.core.JsonGenerator.Feature
* @see com.fasterxml.jackson.databind.SerializationFeature
* @see com.fasterxml.jackson.databind.DeserializationFeature
* @see com.fasterxml.jackson.databind.MapperFeature
*/
public void setFeaturesToDisable(Object... featuresToDisable) {
if (featuresToDisable != null) {
@ -249,7 +241,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -249,7 +241,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
}
}
public void afterPropertiesSet() throws FatalBeanException {
public void afterPropertiesSet() {
if (this.objectMapper == null) {
this.objectMapper = new ObjectMapper();
}
@ -258,7 +251,7 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -258,7 +251,7 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.objectMapper.setDateFormat(this.dateFormat);
}
if (this.serializers != null || this.deserializers != null) {
if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) {
SimpleModule module = new SimpleModule();
addSerializers(module);
addDeserializers(module);
@ -289,26 +282,27 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -289,26 +282,27 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
}
private void configureFeature(Object feature, boolean enabled) {
if (feature instanceof MapperFeature) {
this.objectMapper.configure((MapperFeature) feature, enabled);
if (feature instanceof JsonParser.Feature) {
this.objectMapper.configure((JsonParser.Feature) feature, enabled);
}
else if (feature instanceof DeserializationFeature) {
this.objectMapper.configure((DeserializationFeature) feature, enabled);
else if (feature instanceof JsonGenerator.Feature) {
this.objectMapper.configure((JsonGenerator.Feature) feature, enabled);
}
else if (feature instanceof SerializationFeature) {
this.objectMapper.configure((SerializationFeature) feature, enabled);
}
else if (feature instanceof JsonParser.Feature) {
this.objectMapper.configure((JsonParser.Feature) feature, enabled);
else if (feature instanceof DeserializationFeature) {
this.objectMapper.configure((DeserializationFeature) feature, enabled);
}
else if (feature instanceof JsonGenerator.Feature) {
this.objectMapper.configure((JsonGenerator.Feature) feature, enabled);
else if (feature instanceof MapperFeature) {
this.objectMapper.configure((MapperFeature) feature, enabled);
}
else {
throw new FatalBeanException("Unknown feature class " + feature.getClass().getName());
}
}
/**
* Return the singleton ObjectMapper.
*/

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

@ -1,5 +1,5 @@ @@ -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.
@ -32,8 +32,8 @@ import org.springframework.beans.factory.FactoryBean; @@ -32,8 +32,8 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
/**
* A FactoryBean for creating a Jackson {@link ObjectMapper} with setters to
* enable or disable Jackson features from within XML configuration.
* A {@link FactoryBean} for creating a Jackson 1.x {@link ObjectMapper} with setters
* to enable or disable Jackson features from within XML configuration.
*
* <p>Example usage with MappingJacksonHttpMessageConverter:
* <pre>
@ -79,6 +79,8 @@ import org.springframework.beans.factory.InitializingBean; @@ -79,6 +79,8 @@ import org.springframework.beans.factory.InitializingBean;
* &lt;/bean>
* </pre>
*
* <p><b>NOTE:</b> Requires Jackson 1.8 or higher, as of Spring 4.0.
*
* @author <a href="mailto:dmitry.katsubo@gmail.com">Dmitry Katsubo</a>
* @author Rossen Stoyanchev
* @since 3.2
@ -161,11 +163,10 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean<ObjectMapper> @@ -161,11 +163,10 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean<ObjectMapper>
/**
* Specify features to enable.
*
* @see org.codehaus.jackson.JsonParser.Feature
* @see org.codehaus.jackson.JsonGenerator.Feature
* @see org.codehaus.jackson.map.SerializationConfig.Feature
* @see org.codehaus.jackson.map.DeserializationConfig.Feature
* @see org.codehaus.jackson.map.JsonParser.Feature
* @see org.codehaus.jackson.map.JsonGenerator.Feature
*/
public void setFeaturesToEnable(Object[] featuresToEnable) {
if (featuresToEnable != null) {
@ -177,11 +178,10 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean<ObjectMapper> @@ -177,11 +178,10 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean<ObjectMapper>
/**
* Specify features to disable.
*
* @see org.codehaus.jackson.JsonParser.Feature
* @see org.codehaus.jackson.JsonGenerator.Feature
* @see org.codehaus.jackson.map.SerializationConfig.Feature
* @see org.codehaus.jackson.map.DeserializationConfig.Feature
* @see org.codehaus.jackson.map.JsonParser.Feature
* @see org.codehaus.jackson.map.JsonGenerator.Feature
*/
public void setFeaturesToDisable(Object[] featuresToDisable) {
if (featuresToDisable != null) {
@ -191,41 +191,44 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean<ObjectMapper> @@ -191,41 +191,44 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean<ObjectMapper>
}
}
public void afterPropertiesSet() {
if (this.objectMapper == null) {
this.objectMapper = new ObjectMapper();
}
if (this.annotationIntrospector != null) {
this.objectMapper.getSerializationConfig().setAnnotationIntrospector(annotationIntrospector);
this.objectMapper.getDeserializationConfig().setAnnotationIntrospector(annotationIntrospector);
this.objectMapper.setSerializationConfig(
this.objectMapper.getSerializationConfig().withAnnotationIntrospector(this.annotationIntrospector));
this.objectMapper.setDeserializationConfig(
this.objectMapper.getDeserializationConfig().withAnnotationIntrospector(this.annotationIntrospector));
}
if (this.dateFormat != null) {
// Deprecated for 1.8+, use objectMapper.setDateFormat(dateFormat);
this.objectMapper.getSerializationConfig().setDateFormat(this.dateFormat);
this.objectMapper.setDateFormat(this.dateFormat);
}
for (Map.Entry<Object, Boolean> entry : this.features.entrySet()) {
configureFeature(entry.getKey(), entry.getValue().booleanValue());
configureFeature(entry.getKey(), entry.getValue());
}
}
private void configureFeature(Object feature, boolean enabled) {
if (feature instanceof DeserializationConfig.Feature) {
this.objectMapper.configure((DeserializationConfig.Feature) feature, enabled);
}
else if (feature instanceof SerializationConfig.Feature) {
this.objectMapper.configure((SerializationConfig.Feature) feature, enabled);
}
else if (feature instanceof JsonParser.Feature) {
if (feature instanceof JsonParser.Feature) {
this.objectMapper.configure((JsonParser.Feature) feature, enabled);
}
else if (feature instanceof JsonGenerator.Feature) {
this.objectMapper.configure((JsonGenerator.Feature) feature, enabled);
}
else if (feature instanceof SerializationConfig.Feature) {
this.objectMapper.configure((SerializationConfig.Feature) feature, enabled);
}
else if (feature instanceof DeserializationConfig.Feature) {
this.objectMapper.configure((DeserializationConfig.Feature) feature, enabled);
}
else {
throw new IllegalArgumentException("Unknown feature class: " + feature.getClass().getName());
}
}
/**
* Return the singleton ObjectMapper.
*/

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

@ -39,8 +39,8 @@ import org.springframework.http.converter.HttpMessageNotWritableException; @@ -39,8 +39,8 @@ import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.Assert;
/**
* 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}.
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that
* can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson 2.x's</a> {@link ObjectMapper}.
*
* <p>This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances.
*
@ -178,6 +178,8 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv @@ -178,6 +178,8 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
throws IOException, HttpMessageNotWritableException {
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
// The following has been deprecated as late as Jackson 2.2 (April 2013);
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
JsonGenerator jsonGenerator =
this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
@ -200,8 +202,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv @@ -200,8 +202,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
/**
* Return the Jackson {@link JavaType} for the specified type and context class.
* <p>The default implementation returns {@link ObjectMapper#constructType(java.lang.reflect.Type)}
* or {@code ObjectMapper.getTypeFactory().constructType(type, contextClass)},
* <p>The default implementation returns {@code typeFactory.constructType(type, contextClass)},
* but this can be overridden in subclasses, to allow for custom generic collection handling.
* For instance:
* <pre class="code">
@ -220,9 +221,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv @@ -220,9 +221,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
* @return the java type
*/
protected JavaType getJavaType(Type type, Class<?> contextClass) {
return (contextClass != null) ?
this.objectMapper.getTypeFactory().constructType(type, contextClass) :
this.objectMapper.constructType(type);
return this.objectMapper.getTypeFactory().constructType(type, contextClass);
}
/**

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

@ -1,5 +1,5 @@ @@ -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.
@ -26,7 +26,6 @@ import org.codehaus.jackson.JsonGenerator; @@ -26,7 +26,6 @@ import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.JavaType;
import org.springframework.http.HttpInputMessage;
@ -39,8 +38,8 @@ import org.springframework.http.converter.HttpMessageNotWritableException; @@ -39,8 +38,8 @@ import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.Assert;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
* that can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson's</a> {@link ObjectMapper}.
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that
* can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson 1.x's</a> {@link ObjectMapper}.
*
* <p>This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances.
*
@ -111,7 +110,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve @@ -111,7 +110,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
}
/**
* Whether to use the {@link org.codehaus.jackson.impl.DefaultPrettyPrinter} when writing JSON.
* Whether to use the {@link org.codehaus.jackson.util.DefaultPrettyPrinter} when writing JSON.
* This is a shortcut for setting up an {@code ObjectMapper} as follows:
* <pre>
* ObjectMapper mapper = new ObjectMapper();
@ -125,6 +124,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve @@ -125,6 +124,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
configurePrettyPrint();
}
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return canRead(clazz, null, mediaType);
@ -197,8 +197,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve @@ -197,8 +197,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
/**
* Return the Jackson {@link JavaType} for the specified type and context class.
* <p>The default implementation returns {@link TypeFactory#type(java.lang.reflect.Type)}
* or {@code TypeFactory.type(type, TypeFactory.type(contextClass))},
* <p>The default implementation returns {@code typeFactory.constructType(type, contextClass)},
* but this can be overridden in subclasses, to allow for custom generic collection handling.
* For instance:
* <pre class="code">
@ -216,9 +215,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve @@ -216,9 +215,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
* @return the java type
*/
protected JavaType getJavaType(Type type, Class<?> contextClass) {
return (contextClass != null) ?
TypeFactory.type(type, TypeFactory.type(contextClass)) :
TypeFactory.type(type);
return this.objectMapper.getTypeFactory().constructType(type, contextClass);
}
/**

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

@ -39,7 +39,7 @@ import org.springframework.web.servlet.view.AbstractView; @@ -39,7 +39,7 @@ 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}.
* using <a href="http://jackson.codehaus.org/">Jackson 2.x'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
@ -278,6 +278,8 @@ public class MappingJackson2JsonView extends AbstractView { @@ -278,6 +278,8 @@ public class MappingJackson2JsonView extends AbstractView {
* @throws IOException if writing failed
*/
protected void writeContent(OutputStream stream, Object value, boolean prefixJson) throws IOException {
// The following has been deprecated as late as Jackson 2.2 (April 2013);
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
JsonGenerator generator = this.objectMapper.getJsonFactory().createJsonGenerator(stream, this.encoding);
// A workaround for JsonGenerators not applying serialization features

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

@ -39,7 +39,7 @@ import org.springframework.web.servlet.view.AbstractView; @@ -39,7 +39,7 @@ 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's</a> {@link ObjectMapper}.
* using <a href="http://jackson.codehaus.org/">Jackson 1.x'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

56
spring-webmvc/src/test/java/org/springframework/web/servlet/view/json/MappingJackson2JsonViewTests.java

@ -23,11 +23,22 @@ import java.util.HashSet; @@ -23,11 +23,22 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.core.JsonGenerator;
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.BeanSerializerFactory;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
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.http.MediaType;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
@ -35,20 +46,6 @@ import org.springframework.ui.ModelMap; @@ -35,20 +46,6 @@ import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.View;
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;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
@ -69,6 +66,7 @@ public class MappingJackson2JsonViewTests { @@ -69,6 +66,7 @@ public class MappingJackson2JsonViewTests {
private ScriptableObject jsScope;
@Before
public void setUp() {
request = new MockHttpServletRequest();
@ -80,6 +78,7 @@ public class MappingJackson2JsonViewTests { @@ -80,6 +78,7 @@ public class MappingJackson2JsonViewTests {
view = new MappingJackson2JsonView();
}
@Test
public void isExposePathVars() {
assertEquals("Must not expose path variables", false, view.isExposePathVariables());
@ -87,7 +86,6 @@ public class MappingJackson2JsonViewTests { @@ -87,7 +86,6 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderSimpleMap() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
model.put("foo", "bar");
@ -110,12 +108,10 @@ public class MappingJackson2JsonViewTests { @@ -110,12 +108,10 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderWithSelectedContentType() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", "bar");
view.render(model, request, response);
assertEquals("application/json", response.getContentType());
request.setAttribute(View.SELECTED_CONTENT_TYPE, new MediaType("application", "vnd.example-v2+xml"));
@ -147,7 +143,6 @@ public class MappingJackson2JsonViewTests { @@ -147,7 +143,6 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderSimpleBean() throws Exception {
Object bean = new TestBeanSimple();
Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
@ -164,7 +159,6 @@ public class MappingJackson2JsonViewTests { @@ -164,7 +159,6 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderWithPrettyPrint() throws Exception {
ModelMap model = new ModelMap("foo", new TestBeanSimple());
view.setPrettyPrint(true);
@ -178,14 +172,12 @@ public class MappingJackson2JsonViewTests { @@ -178,14 +172,12 @@ public class MappingJackson2JsonViewTests {
@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);
@ -200,7 +192,6 @@ public class MappingJackson2JsonViewTests { @@ -200,7 +192,6 @@ public class MappingJackson2JsonViewTests {
@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
SerializerFactory factory = new DelegatingSerializerFactory(null);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializerFactory(factory);
@ -314,11 +305,13 @@ public class MappingJackson2JsonViewTests { @@ -314,11 +305,13 @@ public class MappingJackson2JsonViewTests {
}
}
@JsonSerialize(using=TestBeanSimpleSerializer.class)
public static class TestBeanSimpleAnnotated extends TestBeanSimple {
}
public static class TestChildBean {
private String value = "bar";
@ -344,6 +337,7 @@ public class MappingJackson2JsonViewTests { @@ -344,6 +337,7 @@ public class MappingJackson2JsonViewTests {
}
}
public static class TestBeanSimpleSerializer extends JsonSerializer<Object> {
@Override
@ -355,32 +349,22 @@ public class MappingJackson2JsonViewTests { @@ -355,32 +349,22 @@ public class MappingJackson2JsonViewTests {
}
}
public static class DelegatingSerializerFactory extends BasicSerializerFactory {
private SerializerFactory beanSerializer = BeanSerializerFactory.instance;
public static class DelegatingSerializerFactory extends BeanSerializerFactory {
protected DelegatingSerializerFactory(SerializerFactoryConfig config) {
super(config);
}
@Override
public JsonSerializer<Object> createSerializer(SerializerProvider prov, JavaType type, BeanProperty property) throws JsonMappingException {
public JsonSerializer<Object> createSerializer(SerializerProvider prov, JavaType type) throws JsonMappingException {
if (type.getRawClass() == TestBeanSimple.class) {
return new TestBeanSimpleSerializer();
}
else {
return beanSerializer.createSerializer(prov, type, property);
return super.createSerializer(prov, type);
}
}
@Override
public SerializerFactory withConfig(SerializerFactoryConfig config) {
return null;
}
@Override
protected Iterable<Serializers> customSerializers() {
return null;
}
}
}

40
spring-webmvc/src/test/java/org/springframework/web/servlet/view/json/MappingJacksonJsonViewTests.java

@ -24,6 +24,8 @@ import java.util.Map; @@ -24,6 +24,8 @@ import java.util.Map;
import java.util.Set;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.BeanProperty;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
@ -31,11 +33,13 @@ import org.codehaus.jackson.map.SerializerFactory; @@ -31,11 +33,13 @@ 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 org.codehaus.jackson.type.JavaType;
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.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.ui.ModelMap;
@ -60,6 +64,7 @@ public class MappingJacksonJsonViewTests { @@ -60,6 +64,7 @@ public class MappingJacksonJsonViewTests {
private ScriptableObject jsScope;
@Before
public void setUp() {
request = new MockHttpServletRequest();
@ -71,6 +76,7 @@ public class MappingJacksonJsonViewTests { @@ -71,6 +76,7 @@ public class MappingJacksonJsonViewTests {
view = new MappingJacksonJsonView();
}
@Test
public void isExposePathVars() {
assertEquals("Must not expose path variables", false, view.isExposePathVariables());
@ -78,7 +84,6 @@ public class MappingJacksonJsonViewTests { @@ -78,7 +84,6 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderSimpleMap() throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
model.put("foo", "bar");
@ -122,7 +127,6 @@ public class MappingJacksonJsonViewTests { @@ -122,7 +127,6 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderSimpleBean() throws Exception {
Object bean = new TestBeanSimple();
Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
@ -139,7 +143,6 @@ public class MappingJacksonJsonViewTests { @@ -139,7 +143,6 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderWithPrettyPrint() throws Exception {
ModelMap model = new ModelMap("foo", new TestBeanSimple());
view.setPrettyPrint(true);
@ -153,14 +156,12 @@ public class MappingJacksonJsonViewTests { @@ -153,14 +156,12 @@ public class MappingJacksonJsonViewTests {
@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);
@ -175,8 +176,7 @@ public class MappingJacksonJsonViewTests { @@ -175,8 +176,7 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
SerializerFactory factory = new DelegatingSerializerFactory();
SerializerFactory factory = new DelegatingSerializerFactory(null);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializerFactory(factory);
view.setObjectMapper(mapper);
@ -197,7 +197,6 @@ public class MappingJacksonJsonViewTests { @@ -197,7 +197,6 @@ public class MappingJacksonJsonViewTests {
@Test
public void renderOnlyIncludedAttributes() throws Exception {
Set<String> attrs = new HashSet<String>();
attrs.add("foo");
attrs.add("baz");
@ -288,11 +287,12 @@ public class MappingJacksonJsonViewTests { @@ -288,11 +287,12 @@ public class MappingJacksonJsonViewTests {
}
}
@JsonSerialize(using=TestBeanSimpleSerializer.class)
public static class TestBeanSimpleAnnotated extends TestBeanSimple {
}
public static class TestChildBean {
private String value = "bar";
@ -318,10 +318,11 @@ public class MappingJacksonJsonViewTests { @@ -318,10 +318,11 @@ public class MappingJacksonJsonViewTests {
}
}
public static class TestBeanSimpleSerializer extends JsonSerializer<TestBeanSimple> {
public static class TestBeanSimpleSerializer extends JsonSerializer<Object> {
@Override
public void serialize(TestBeanSimple value, JsonGenerator jgen, SerializerProvider provider)
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
throws IOException {
jgen.writeStartObject();
@ -332,19 +333,22 @@ public class MappingJacksonJsonViewTests { @@ -332,19 +333,22 @@ public class MappingJacksonJsonViewTests {
}
}
public static class DelegatingSerializerFactory extends SerializerFactory {
private SerializerFactory delegate = BeanSerializerFactory.instance;
public static class DelegatingSerializerFactory extends BeanSerializerFactory {
public DelegatingSerializerFactory(Config config) {
super(config);
}
@Override
@SuppressWarnings("unchecked")
public <T> JsonSerializer<T> createSerializer(Class<T> type, SerializationConfig config) {
if (type == TestBeanSimple.class) {
return (JsonSerializer<T>) new TestBeanSimpleSerializer();
public JsonSerializer<Object> createSerializer(SerializationConfig config, JavaType baseType, BeanProperty property) throws JsonMappingException {
if (baseType.getRawClass() == TestBeanSimple.class) {
return new TestBeanSimpleSerializer();
}
else {
return delegate.createSerializer(type, config);
return super.createSerializer(config, baseType, property);
}
}
}
}

Loading…
Cancel
Save