解决springboot+mybatisplus返回时间格式带T

文章讨论了在SpringBoot2.3.4中处理LocalDateTime类型数据的问题,介绍了如何在YAML配置文件中设置日期格式和自定义序列化器的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原因:我service实现类的代码是

@Override
	public Map<String, Object> queryDictPage(Map<String, Object> queryMap) {
		Map<String,Object> map = new HashMap<>();
		QueryWrapper<Dict> wrapper = new QueryWrapper<>();
//        IPage<Dict> iPage = basesMapper.selectPage(page, wrapper);
//        List<Map<String, Object>> list = ConvertUtils.objectsToMaps(iPage.getRecords());
		Page<Map> page = new Page<>((Integer)queryMap.get("page"), (Integer)queryMap.get("limit"));
		IPage<Map<String, Object>> iPage = basesMapper.queryDictPage(page, queryMap);
		
		map.put("code",0);
		map.put("msg","");
		map.put("count",iPage.getTotal());
		map.put("data",iPage.getRecords());
		return map;
	}

对,IPage<Map<String, Object> page  是map,从page.records 中找到查询数据库返回的数据。其中crateTime是LocalDateTime类型

而我最终返回的也是map(所以去实体类加

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")这些是没用的。

)。

springboot版本是2.3.4.RELEASE,返回前端的时候也没有做处理,即使在applicaton.yml配置了

也没效果。

大概是该版本没有考虑 LocalDateTime类型的转换,常用的时间类型,该配置是有效的。好了,大概就这样吧,不深入了解了,没兴趣。

解决办法:

1、在application.yml中配置(这个也得要,常用的date类型还是有效的)

spring:
  profiles:
    active: dev
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

2、配置LocalDataTime的处理(感谢其他csdn作者提供,找不到你链接了)

package com.wms.config;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 处理mybatisplus 转时间字段为LocalDateTime类型,而springboot2.3.4.RELEASE
 */
@Configuration
public class LocalDateTimeSerializerConfig {

    @Value("${spring.jackson.date-format}")
    private String pattern;

    @Bean
    public LocalDateTimeSerializer localDateTimeDeserializer() {
        return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值