Skip to content

spring.jackson.date-format configuration does not affect serialization of Date #6642

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

Closed
pavelfomin opened this issue Aug 13, 2016 · 4 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@pavelfomin
Copy link

pavelfomin commented Aug 13, 2016

I'm having an issue similar to a one described by @csavory in #2225 - if I add a WebConfig that extends WebMvcConfigurerAdapter then my date format specified for jackson in application.properties is not taking effect:

spring.jackson.date-format=yyyy-MM-dd'T'HH:mm:ss.SSSZ
spring.jackson.serialization.write_dates_as_timestamps=false

I'm using boot spring boot 1.4.0. Example of failing test case is at https://github.com/pavelfomin/spring-boot-rest-example (branch feature/actuator-only):

mockMvc.perform(get("/info"))
        .andExpect(status().isOk())
        .andExpect(content().contentType(JSON_MEDIA_TYPE))
        .andExpect(jsonPath("$.build", isA(Object.class)))
        .andExpect(jsonPath("$.build.version", isA(String.class)))
        .andExpect(jsonPath("$.build.artifact", is("spring-boot-rest-example")))
        .andExpect(jsonPath("$.build.group", is("com.droidablebee")))
        .andExpect(jsonPath("$.build.time", isA(String.class)))
        ;

The build.time field is expected to be formatted according to spring.jackson.date-format. If I comment out @Configuration in WebConfiguration which extends WebMvcConfigurerAdapter then the test case passes.

How to reproduce:

  • git clone --branch feature/actuator-only https://github.com/pavelfomin/spring-boot-rest-example.git
  • mvn clean test
  • test fails

The test run results in the following error:

Expected: is an instance of java.lang.String
     but: <1471056396151L> is a java.lang.Long
  • comment out @Configuration in WebConfiguration
  • mvn clean test
  • test passes
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 13, 2016
@wilkinsona
Copy link
Member

Your web configuration class is annotated with @EnableWebMvc which as documented allows you to "take complete control of Spring MVC". Taking complete control means Boot no longer configures Spring MVC's Jackson-based HTTP message converter.

You should remove the @EnableWebMvc annotation, or configure the HTTP message converters manually.

@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 13, 2016
pavelfomin added a commit to pavelfomin/spring-boot-rest-example that referenced this issue Aug 13, 2016
Taking complete control means Boot no longer configures Spring MVC's
Jackson-based HTTP message converter.
spring-projects/spring-boot#6642
@hatavan83
Copy link

hatavan83 commented Sep 21, 2016

I have the same problem when I config jackson with spring.jackson.default-property-inclusion=non_empty. Jackson cannot auto config by application.property in Spring MVC, it only effect when I add spring-boot-starter-data-rest to dependency

The root cause is here http://stackoverflow.com/questions/32654854/the-spring-jackson-serialization-inclusion-environment-setting-does-not-seem-to

@zhaozhi406
Copy link

hatavan's solution is easy and directly, but it's not the root cause, though add spring-boot-starter-data-rest in your classpath solve the problem; the root cause is that when spring boot init default MappingJackson2HttpMessageConverter it does'n use the Jackson2ObjectMapperBuilder instance which created by JacksonAutoConfiguration, this is because Jackson2ObjectMapperBuilderCustomizerConfiguration has an order(0) priority.

@wilkinsona
Copy link
Member

@zhaozhi406 The root cause in this issue was the use of @EnableWebMvc and the solution is to remove it. I understand that you are trying to help, and thank you for that, but adding Spring Data REST and doing what you propose on Stack Overflow should not be necessary so you may be misleading people.

The order on the customiser is 0 to allow user-provided customisers to go before and after it. It will have no effect on what does and does not use the auto-configured ObjectMapper.

If you have found a situation where what you have described on Stack Overflow is necessary, please open a new issue with a small sample that reproduces the problem. What you are doing should not be necessary so there may be a bug that we should fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

5 participants