json-view icon indicating copy to clipboard operation
json-view copied to clipboard

JsonViewSupportFactoryBean breaks jackson-datatype-jsr310

Open johnloven opened this issue 7 years ago • 4 comments

I would first like to say that what you've done here is brilliant; it was exactly what I was looking for.

However, when I define the @Bean as you suggest in the README, my LocalDateTime fields are no longer correctly serialized. I use jackson-datatype-jsr310 in order to serialize these fields in the correct way, but when I write:

@Bean
public JsonViewSupportFactoryBean views() {
    return new JsonViewSupportFactoryBean();
}

this no longer works and I get a very weird JSON blob from the serialization.

My gradle dependencies are the following:

dependencies {
    compile("com.h2database:h2")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-devtools")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile('org.springframework.security.oauth:spring-security-oauth2')
    compile('org.springframework.security:spring-security-jwt')
    compile('org.springframework.hateoas:spring-hateoas')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('com.google.api-client:google-api-client:1.20.0')
    compile('com.google.apis:google-api-services-gmail:v1-rev67-1.22.0')
    compile("org.springframework.boot:spring-boot-starter-mail")
    compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.9')
    compile('org.hibernate:hibernate-core:5.2.10.Final')
    compile('com.monitorjbl:spring-json-view:0.14') {
        exclude group: "org.slf4j", module: "slf4j-log4j12"
    }
}

Do you have any ideas why this is happening?

johnloven avatar Aug 09 '17 14:08 johnloven

That's odd, it should be supported. There's code specifically delegating JSR310 serialization here: https://github.com/monitorjbl/json-view/blob/master/json-view/src/main/java/com/monitorjbl/json/JsonViewSerializer.java#L186. there's also a test case for it here: https://github.com/monitorjbl/json-view/blob/master/json-view/src/test/java/com/monitorjbl/json/JsonViewSerializerTest.java#L779 .

Did you only include the JsonViewModule in your mapper? You need to include both if you haven't.

monitorjbl avatar Aug 09 '17 15:08 monitorjbl

I'm sure my shallow knowledge of the Spring Framework is the root of the problem here..

I managed to get the result I wanted by initializing JsonViewSupportFactoryBean with an injected ObjectMapper. But now I've refactored my classes and have the following in the same @Configuration class:

@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    return builder
            .createXmlMapper(false)
            .build()
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}

@Autowired
private MappingJackson2HttpMessageConverter converter;

@Bean
public JsonViewSupportFactoryBean views() {
    return new JsonViewSupportFactoryBean(converter.getObjectMapper());
}

In my opinion this looks quite odd and I'm sure there is a better way to do this. Internet seems a bit confused as to how the ObjectMapper in Spring Boot/Jackson is/should be used (is it a singleton? can it be injected into other classes?).

Would you care to shed some light on this for me, oh great one?

johnloven avatar Aug 17 '17 08:08 johnloven

My guess is that those are not the same ObjectMapper objects. You might want to try explicitly creating another mapper with the correct properties to see if that fixes the problem. If it does, you can find some way to write in the ObjectMapper instance you made in the objectMapper() method.

monitorjbl avatar Aug 23 '17 03:08 monitorjbl

I've had the same issue. Is it possible that

@Bean
public JsonViewSupportFactoryBean views() {
    // no object mapper in constructor
    return new JsonViewSupportFactoryBean();
}

overwrites the spring configuration for jackson?

roberterdin avatar Dec 04 '17 16:12 roberterdin