In my Spring REST web application, I am trying to get a controller working with Java8 LocalTime. I am sending this Json in a POST request
{
"hour": "0",
"minute": "0",
"second": "0",
"nano": "0"
}
and I get a HttpMessageNotReadableExceptionwith the next Jackson error
Could not read JSON: No suitable constructor found for type [simple type, class java.time.LocalTime]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: org.apache.catalina.connector.CoyoteInputStream@58cfa2a0; line: 2, column: 5];
nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class java.time.LocalTime]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: org.apache.catalina.connector.CoyoteInputStream@58cfa2a0; line: 2, column: 5]
I am using spring -web-4.0.3.RELEASE with spring-boot-starter-web-1.0.2.RELEASE, jackson-databind-2.4.2 and jackson-datatype-jsr310-2.4 0.2
From what I understood, googling around, Spring should automatically register JSR-310 modules for Java8.time objects.
I found the answer to my problem, but it didn’t work for me: Spring Boot and Jackson, JSR310 in the response body
I have no configurations annotated with @EnableWebMvc and here is my only configuration class
@EnableAutoConfiguration
@ComponentScan
@EnableAspectJAutoProxy()
@Configuration
@ImportResource(value = "Beans.xml")
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToLocalDateTime());
registry.addConverter(new LocalDateTimeToString());
registry.addConverter(new StringToLocalDate());
registry.addConverter(new LocalDateToString());
registry.addConverter(new StringToLocalTime());
registry.addConverter(new LocalTimeToString());
}
}
Can you guess what is wrong in my configuration?