parent
7317457bb1
commit
c114c08922
|
@ -40,7 +40,6 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
|||
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 class Jackson2ObjectMapperBuilder {
|
|||
* 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 class Jackson2ObjectMapperBuilder {
|
|||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
|
|||
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.TypeIdResolver;
|
|||
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.annotation.AutowiredAnnotationBeanPostP
|
|||
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 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
|
|||
public class SpringHandlerInstantiatorTests {
|
||||
|
||||
private SpringHandlerInstantiator instantiator;
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
|
@ -78,6 +80,7 @@ public class SpringHandlerInstantiatorTests {
|
|||
objectMapper = Jackson2ObjectMapperBuilder.json().handlerInstantiator(instantiator).build();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void autowiredSerializer() throws JsonProcessingException {
|
||||
User user = new User("bob");
|
||||
|
@ -113,6 +116,7 @@ public class SpringHandlerInstantiatorTests {
|
|||
assertTrue(CustomTypeIdResolver.isAutowiredFiledInitialized);
|
||||
}
|
||||
|
||||
|
||||
public static class UserDeserializer extends JsonDeserializer<User> {
|
||||
|
||||
@Autowired
|
||||
|
@ -124,9 +128,9 @@ public class SpringHandlerInstantiatorTests {
|
|||
JsonNode node = oc.readTree(jsonParser);
|
||||
return new User(this.capitalizer.capitalize(node.get("username").asText()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class UserSerializer extends JsonSerializer<User> {
|
||||
|
||||
@Autowired
|
||||
|
@ -140,6 +144,7 @@ public class SpringHandlerInstantiatorTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public static class UpperCaseKeyDeserializer extends KeyDeserializer {
|
||||
|
||||
@Autowired
|
||||
|
@ -151,6 +156,7 @@ public class SpringHandlerInstantiatorTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public static class CustomTypeResolverBuilder extends StdTypeResolverBuilder {
|
||||
|
||||
@Autowired
|
||||
|
@ -170,6 +176,7 @@ public class SpringHandlerInstantiatorTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public static class CustomTypeIdResolver implements TypeIdResolver {
|
||||
|
||||
@Autowired
|
||||
|
@ -178,7 +185,6 @@ public class SpringHandlerInstantiatorTests {
|
|||
public static boolean isAutowiredFiledInitialized = false;
|
||||
|
||||
public CustomTypeIdResolver() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -204,15 +210,20 @@ public class SpringHandlerInstantiatorTests {
|
|||
|
||||
@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 class SpringHandlerInstantiatorTests {
|
|||
public String getUsername() { return this.username; }
|
||||
}
|
||||
|
||||
|
||||
public static class SecurityRegistry {
|
||||
|
||||
@JsonDeserialize(keyUsing = UpperCaseKeyDeserializer.class)
|
||||
|
@ -243,6 +255,7 @@ public class SpringHandlerInstantiatorTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "type")
|
||||
@JsonTypeResolver(CustomTypeResolverBuilder.class)
|
||||
@JsonTypeIdResolver(CustomTypeIdResolver.class)
|
||||
|
@ -255,7 +268,6 @@ public class SpringHandlerInstantiatorTests {
|
|||
}
|
||||
|
||||
public Group() {
|
||||
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
|
@ -263,10 +275,12 @@ public class SpringHandlerInstantiatorTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public static class Capitalizer {
|
||||
|
||||
public String capitalize(String text) {
|
||||
return text.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue