SPRING MVC DateTimeFormat错误 [英] SPRING MVC DateTimeFormat error

查看:112
本文介绍了SPRING MVC DateTimeFormat错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring + Angular JS创建一个网站,而我当前的问题是我使用POSTMAN发布了JSON语句,如下所示:

Im trying to make a website using Spring + Angular JS and my current problem is i used POSTMAN to post a JSON statement as below:

{
    "id": 12345,
    "checkin" : "2017-03-01",
    "checkout" : "2017-03-05"
}

,然后弹出此错误:
在此处输入图片描述

类型定义错误:[简单类型,类java.time.LocalDate];嵌套异常为com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造 java.time.LocalDate 的实例(无创建者,像默认构造一样,存在):没有可从字符串值反序列化的字符串参数构造函数/工厂方法('2017-03-01')

"Type definition error: [simple type, class java.time.LocalDate]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.LocalDate (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2017-03-01')"

资源代码:

@RequestMapping(path = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
        consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<ReservationResponse> createReservation(
        @RequestBody
        ReservationRequest reservationRequest){
    return new ResponseEntity<>(new ReservationResponse(), HttpStatus.CREATED);
}

标准代码:

public class ReservationRequest {
    private Long id;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private LocalDate checkin;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private LocalDate checkout;}

APIConfig代码:

APIConfig Code:

@Bean
@Primary
public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModules(new JavaTimeModule());
    return new ObjectMapper();
}

应用程序属性:

spring.jackson.serialization.write-dates-as-timestamps=false

Build Gradle:

Build Gradle:

compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.8.7'


推荐答案

我们也面临同样的问题,在定义转换器后,它可以正常工作。您可以在应用程序中添加以下代码,它应该可以正常工作。您可以根据需要更改日期格式。

We also faced the same issue and after defining a converter it worked fine. You can add below code in the application and it should work fine. You can change the date format as per your need.

@Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
       MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
       ObjectMapper objectMapper = new ObjectMapper();
          objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
          objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, true);
          objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
          objectMapper.setDateFormat(new SimpleDateFormat("dd MMM yyyy hh:mm:ss a"));
      jsonConverter.setObjectMapper(objectMapper);

      return jsonConverter;
     }

这篇关于SPRING MVC DateTimeFormat错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆