Spring web:@RequestBody Json转换为Java8 LocalTime无法正常工作 [英] Spring web: @RequestBody Json conversion for Java8 LocalTime not working
问题描述
在我的Spring REST webapp中,我正在尝试让控制器使用Java8 LocalTime。
我在POST请求中发送此Json
In my Spring REST webapp, I'm trying to get a controller working with Java8 LocalTime. I'm sending this Json in a POST request
{
"hour": "0",
"minute": "0",
"second": "0",
"nano": "0"
}
我得到一个 HttpMessageNotReadableException
,其中包含以下Jackson错误
and I get a HttpMessageNotReadableException
with the following 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]
我正在使用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
I'm 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.2
从我的内容知道谷歌搜索,Spring应该自动为Java8.time对象注册JSR-310模块。
From what I understood googling around, Spring should automatically register JSR-310 modules for Java8.time objects.
我找到了问题的答案,但它对我不起作用:< a href =https://stackoverflow.com/questions/26329875/spring-boot-and-jackson-jsr310-in-response-body> Spring Boot和Jackson,JSR310作为回应主体
I found an answer to my problem, but it does not work for me: Spring Boot and Jackson, JSR310 in response body
我没有使用@EnableWebMvc注释的配置,这是我唯一的配置类
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 suggest what's wrong in my configuration?
推荐答案
您的配置没有任何问题。
There is nothing wrong with your configuration.
我建议您将Spring Boot更新为1.2.0.RELEASE。它使用Spring 4.1.3软件包,解决了 MappingJackson2HttpMessageConverter
的问题。要获得更多信息,请阅读: https://github.com/spring -projects / spring-boot / issues / 1620#issuecomment-58016018 。它适用于我的情况。
I suggest you to update Spring Boot to 1.2.0.RELEASE. It uses Spring 4.1.3 packages, which have resolved issue with MappingJackson2HttpMessageConverter
. To get more info read: https://github.com/spring-projects/spring-boot/issues/1620#issuecomment-58016018 . It worked in my case.
如果您不想更新Spring Boot版本,可能至少可以注释 LocalTime
的字段显式如下:
If you do not want to update Spring Boot version, probably the least you can do is to annotate LocalTime
's fields explicitly like that:
@JsonSerialize(using = DateTimeSerializer.class)
@JsonDeserialize(using = DateTimeDeserializer.class)
private LocalTime date;
这篇关于Spring web:@RequestBody Json转换为Java8 LocalTime无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!