Skip to content
Advertisement

Spring Boot unable to set global date format for REST API input

I am trying to set a global format for date-time, so that I don’t have to annotate each and every method or on DTO fields.

I have tried to configure it globally, the only impact it had was on API response, it is formatting the date in specific pattern.

But it does not accept it as an input format.

JavaScript

Exception message

JavaScript

A REST controller method where the call lands is as follow

JavaScript

My expectaion from above configuration is, it should work for both input and output of the API

  1. It must accept the format I specificy for input
  2. It must accept null as value

Everything works fine if I use @DateTimeFormat annotation, which means for each field I have to make such changes

JavaScript

Advertisement

Answer

you should use

JavaScript

AbstractNamedValueMethodArgumentResolver#resolveArgument use WebDataBinder,WebDataBinder use ConversionService registered in WebMvcAutoConfiguration.EnableWebMvcConfiguration#mvcConversionService, and this converter will be registered in org.springframework.boot.convert.ApplicationConversionService#addBeans

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement