diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java index f975efdb7090..c48947a3034d 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java @@ -40,7 +40,6 @@ import com.fasterxml.jackson.databind.cfg.HandlerInstantiator; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.dataformat.xml.XmlMapper; -import javafx.application.Application; import org.springframework.beans.BeanUtils; import org.springframework.beans.FatalBeanException; @@ -441,6 +440,7 @@ public T build() { * settings. This can be applied to any number of {@code ObjectMappers}. * @param objectMapper the ObjectMapper to configure */ + @SuppressWarnings("deprecation") public void configure(ObjectMapper objectMapper) { Assert.notNull(objectMapper, "ObjectMapper must not be null"); @@ -495,13 +495,15 @@ public void configure(ObjectMapper objectMapper) { objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy); } for (Class target : this.mixIns.keySet()) { + // Deprecated as of Jackson 2.5, but just in favor of a fluent variant. objectMapper.addMixInAnnotations(target, this.mixIns.get(target)); } if (this.handlerInstantiator != null) { objectMapper.setHandlerInstantiator(this.handlerInstantiator); } else if (this.applicationContext != null) { - objectMapper.setHandlerInstantiator(new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory())); + objectMapper.setHandlerInstantiator( + new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory())); } } diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/SpringHandlerInstantiatorTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/SpringHandlerInstantiatorTests.java index 71435a1f4ead..7b24d78ed54b 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/SpringHandlerInstantiatorTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/SpringHandlerInstantiatorTests.java @@ -26,6 +26,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.databind.DatabindContext; import com.fasterxml.jackson.databind.DeserializationConfig; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JavaType; @@ -46,9 +47,6 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder; import com.fasterxml.jackson.databind.type.TypeFactory; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; @@ -57,6 +55,8 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; +import static org.junit.Assert.*; + /** * Test class for {@link SpringHandlerInstantiatorTests}. * @@ -65,8 +65,10 @@ public class SpringHandlerInstantiatorTests { private SpringHandlerInstantiator instantiator; + private ObjectMapper objectMapper; + @Before public void setup() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); @@ -78,6 +80,7 @@ public void setup() { objectMapper = Jackson2ObjectMapperBuilder.json().handlerInstantiator(instantiator).build(); } + @Test public void autowiredSerializer() throws JsonProcessingException { User user = new User("bob"); @@ -113,6 +116,7 @@ public void applicationContextAwareTypeIdResolver() throws JsonProcessingExcepti assertTrue(CustomTypeIdResolver.isAutowiredFiledInitialized); } + public static class UserDeserializer extends JsonDeserializer { @Autowired @@ -124,9 +128,9 @@ public User deserialize(JsonParser jsonParser, DeserializationContext deserializ JsonNode node = oc.readTree(jsonParser); return new User(this.capitalizer.capitalize(node.get("username").asText())); } - } + public static class UserSerializer extends JsonSerializer { @Autowired @@ -140,6 +144,7 @@ public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider } } + public static class UpperCaseKeyDeserializer extends KeyDeserializer { @Autowired @@ -151,6 +156,7 @@ public Object deserializeKey(String key, DeserializationContext context) throws } } + public static class CustomTypeResolverBuilder extends StdTypeResolverBuilder { @Autowired @@ -170,6 +176,7 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, Java } } + public static class CustomTypeIdResolver implements TypeIdResolver { @Autowired @@ -178,7 +185,6 @@ public static class CustomTypeIdResolver implements TypeIdResolver { public static boolean isAutowiredFiledInitialized = false; public CustomTypeIdResolver() { - } @Override @@ -204,15 +210,20 @@ public String idFromValue(Object value) { @Override public void init(JavaType type) { - } @Override public String idFromBaseType() { return null; } + + // New in Jackson 2.5 + public JavaType typeFromId(DatabindContext context, String id) { + return null; + } } + @JsonDeserialize(using = UserDeserializer.class) @JsonSerialize(using = UserSerializer.class) public static class User { @@ -229,6 +240,7 @@ public User(String username) { public String getUsername() { return this.username; } } + public static class SecurityRegistry { @JsonDeserialize(keyUsing = UpperCaseKeyDeserializer.class) @@ -243,6 +255,7 @@ public Map getCredentials() { } } + @JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "type") @JsonTypeResolver(CustomTypeResolverBuilder.class) @JsonTypeIdResolver(CustomTypeIdResolver.class) @@ -255,7 +268,6 @@ public Group(String name) { } public Group() { - } public String getType() { @@ -263,10 +275,12 @@ public String getType() { } } + public static class Capitalizer { public String capitalize(String text) { return text.toUpperCase(); } } + }