2

I Cretae one springboot(2.0.6.RELEASE) and put in my application.yml the configuration:

jackson:
    date-format: dd/MM/yyyy

so when my user call my API every date return just like this:

"vencimento": "01/07/2017" 

so, now I put an CORS in my project

@Configuration
@EnableWebMvc
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

but now, all my date filed return just like this:

"vencimento": [
            2017,
            1,
            7
        ],

anyone know why? and how to fix this? tks

6
  • 1
    Are you sure the issue is caused by the CORS? Did you change something else? Commented Feb 28, 2019 at 19:13
  • Seems odd that Cors configuration would cause this problem. What's your controller look like? Commented Feb 28, 2019 at 19:13
  • @CROSP i remove the CORS return to "vencimento": "01/07/2017" Commented Feb 28, 2019 at 19:17
  • 4
    @FabioEbner, if you use @EnableWebMvc in a Spring Boot app you switch off the default settings in the framework, so maybe you should avoid that. Commented Feb 28, 2019 at 19:49
  • 1
    @CROSP a question I only use to create RESTAPI so I don`t need to use @EnableWebMvc correct? tks Commented Feb 28, 2019 at 19:51

2 Answers 2

5

With adding @EnableWebMvc you're saying to Spring that you want to get the full control over Spring MVC configuration. So you need to configure the Jackson object mapper manually.

The main thing is to add these settings:

objectMapper.registerModule(new JavaTimeModule());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

By default, SerializationFeature.WRITE_DATES_AS_TIMESTAMPS is enabled, and it gives an array of date-time components similar to yours (f.e., [2014,3,30,12,30,23,123456789] instead of "2014-03-30T12:30:23.123456789").

The example of how to configure object mapper and register it as bean in @Configuration class (if you don't familiar with how to do it):

    @Bean
    public ObjectMapper jsonObjectMapper() {
        final ObjectMapper jsonMapper = new ObjectMapper();
        jsonMapper.registerModule(new JavaTimeModule());
        jsonMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        //some other configuration like:
        jsonMapper.registerModule(new Jdk8Module());
        jsonMapper.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);

        return jsonMapper;
    }
Sign up to request clarification or add additional context in comments.

Comments

-1

As CROSP said that enabling @EnableWebMvc will turn off default settings in Spring Boot. Here is how I config CORS without using @EnableWebMvc annotation:

@Configuration
class CorsConfig {

  @Bean
  fun corsConfigurer() = object : WebMvcConfigurer {
    override fun addCorsMappings(registry: CorsRegistry) {
      registry.addMapping("/**").allowedOrigins("*")
      super.addCorsMappings(registry)
    }
  }
}

Reference: Spring Guide

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.