Spring @ResponseBody Jackson JsonSerializer with JodaTime

Although you can put an annotation for each date field, is better to do a global configuration for your object mapper. If you use jackson you can configure your spring as follow: <bean id=”jacksonObjectMapper” class=”com.company.CustomObjectMapper” /> <bean id=”jacksonSerializationConfig” class=”org.codehaus.jackson.map.SerializationConfig” factory-bean=”jacksonObjectMapper” factory-method=”getSerializationConfig” > </bean> For CustomObjectMapper: public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper() { super(); … Read more

Make Jackson use a custom deserializer everywhere (for a type which isn’t mine)

LongJsonDeserializer deserializer = new LongJsonDeserializer(); SimpleModule module = new SimpleModule(“LongDeserializerModule”, new Version(1, 0, 0, null, null, null)); module.addDeserializer(Long.class, deserializer); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(module); Here’s a full demo application. This works with the latest release of Jackson, and probably also with Jackson versions going back to 1.7. import java.io.IOException; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonProcessingException; import … Read more

Autowiring in JsonDeserializer: SpringBeanAutowiringSupport vs HandlerInstantiator

Adding to the Amir Jamak’s answer, you don’t have to create custom HandlerInstantiator as Spring has it already which is SpringHandlerInstantiator. What you need to do is to hook it up to Jackson2ObjectMapperBuilder in Spring configuration. @Bean public HandlerInstantiator handlerInstantiator(ApplicationContext applicationContext) { return new SpringHandlerInstantiator(applicationContext.getAutowireCapableBeanFactory()); } @Bean public Jackson2ObjectMapperBuilder objectMapperBuilder(HandlerInstantiator handlerInstantiator) { Jackson2ObjectMapperBuilder builder = … Read more

How to customise Jackson in Spring Boot 1.4

To customize the Jackson ObjectMapper that’s already pre-configured by Spring Boot, I was able to do this (the example here is to add a custom deserializer). Configuration class: @SpringBootConfiguration public class Application { @Autowired private BigDecimalDeserializer bigDecimalDeserializer; … @Bean public Jackson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization() { return new Jackson2ObjectMapperBuilderCustomizer() { @Override public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) { jacksonObjectMapperBuilder.deserializerByType(BigDecimal.class, bigDecimalDeserializer); … Read more

Jackson JSON Deserialization: array elements in each line

If you don’t want to extend DefaultPrettyPrinter you can also just set the indentArraysWith property externally: ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE); String json = objectMapper.writer(prettyPrinter).writeValueAsString(object);

Spark2.1.0 incompatible Jackson versions 2.7.6

Spark 2.1.0 contains com.fasterxml.jackson.core as transitive dependency. So, we do not need to include then in libraryDependencies. But if you want to add a different com.fasterxml.jackson.core dependencies’ version then you have to override them. Like this: name := “testSpark2” version := “1.0” scalaVersion := “2.11.8” dependencyOverrides += “com.fasterxml.jackson.core” % “jackson-core” % “2.8.7” dependencyOverrides += “com.fasterxml.jackson.core” … Read more

Jackson – Deserialize Generic class variable

You will need to build JavaType explicitly, if generic type is only dynamically available: // do NOT create new ObjectMapper per each request! final ObjectMapper mapper = new ObjectMapper(); public Json<T> void deSerialize(Class<T> clazz, InputStream json) { return mapper.readValue(json, mapper.getTypeFactory().constructParametricType(Json.class, clazz)); }