Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonViewSupportFactoryBean breaks jackson-datatype-jsr310 #49

Open
johnloven opened this issue Aug 9, 2017 · 4 comments
Open

JsonViewSupportFactoryBean breaks jackson-datatype-jsr310 #49

johnloven opened this issue Aug 9, 2017 · 4 comments

Comments

@johnloven
Copy link

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?

@monitorjbl
Copy link
Owner

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.

@johnloven
Copy link
Author

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?

@monitorjbl
Copy link
Owner

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.

@roberterdin
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants