【发布时间】:2015-02-08 22:26:21
【问题描述】:
在我的 Spring REST webapp 中,我试图让控制器使用 Java8 LocalTime。 我在 POST 请求中发送这个 Json
{
"hour": "0",
"minute": "0",
"second": "0",
"nano": "0"
}
我得到一个 HttpMessageNotReadableException 并出现以下杰克逊错误
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]
我将 spring-web-4.0.3.RELEASE 与 spring-boot-starter-web-1.0.2.RELEASE、jackson-databind-2.4.2 和 jackson-datatype-jsr310-2.4.2 一起使用
据我了解,Spring 应该自动为 Java8.time 对象注册 JSR-310 模块。
我找到了问题的答案,但它对我不起作用:Spring Boot and Jackson, JSR310 in response body
我没有使用 @EnableWebMvc 注释的配置,这是我唯一的配置类
@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());
}
}
你能建议我的配置有什么问题吗?
【问题讨论】:
-
人们花时间帮助您并回答问题。请花时间接受答案。
标签: json spring-mvc java-8 spring-boot java-time