LocalTime、LocalDate、LocalDateTime格式使用注意事项

  1. 报错: No primary or default constructor found for class java.time.LocalTime
  2. 报错: ailed to convert value of type ‘java.lang.String’ to required type 'java.tim
  3. 报错: Could not read JSON: Cannot construct instance of java.time.LocalTime (no Creators, like default c

解决办法:
任意一个配置类中加上以下代码,解决一些序列化问题:

 @Bean
    @Primary
    public GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer() {
        ObjectMapper om = new ObjectMapper();
        // 解决查询缓存转换异常的问题
        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 此项必须配置,否则会报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to XXX
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        om.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
        // 支持 jdk 1.8 日期   ---- start ---
        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        om.registerModule(new Jdk8Module())
                .registerModule(new JavaTimeModule())
                .registerModule(new ParameterNamesModule());
        // --end --
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(om);
        return genericJackson2JsonRedisSerializer;
    }

直接把导的依赖也发下吧:

// An highlighted block
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>

1:Postman传参,要选择json、raw(不然你怎么修改时间数据都是参数格式失败),LocalDate,LocalDateTime同理
2:实体类加上以下代码,LocalDate,LocalDateTime同理

 @DateTimeFormat(pattern = "HH:mm")
 @JsonFormat(pattern = "HH:mm")//这俩用了定义格式
 @JsonDeserialize(using = LocalTimeDeserializer.class)
 @JsonSerialize(using = LocalTimeSerializer.class)、、这俩解决序列化问题,不然存redis会报错

3:Controller请求参数,将LocalTime的字段放入实体类(可自建DTO),就可以解决,LocalDate,LocalDateTime同理

// 
@RequestBody(required = false)TimeSlotDto timeSlotDto

4:Controller请求LocalTime参数前,加上 @DateTimeFormat(pattern = “HH:mm”)
@RequestParam(required = false)。 LocalDate,LocalDateTime同理

// 
@RequestParam(required = false) @DateTimeFormat(pattern = "HH:mm")LocalTime startTime,@RequestParam(required = false) @DateTimeFormat(pattern = "HH:mm")LocalTime endTime

备注:
1、请求Controller的LocalTime前最好加上以下注解

@RequestBody(required = false))
@RequestParam(required = false))

2、swagger的配置中最好加上:.directModelSubstitute(LocalTime.class,String.class),以方便使用swagger测试。LocalDate,LocalDateTime同理。

附swagger配置文件:

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .directModelSubstitute(LocalTime.class,String.class)//就是加这一行!!!
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值