当前位置: 首页 > article >正文

解决LocalDateTime传输前端为时间的数组

问题出现如下:

问题出现原因:

默认序列化情况下会使用SerializationFeature.WRITE_DATES_AS_TIMESTAMPS。使用这个解析时就会打印出数组。

解决方法:

我在全文搜索处理方法总结如下:

1.前端自定义函数来书写
        ,cols: [[ //表头
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'purchaseId', title: 'ID',  sort: true, fixed: 'left',hide:true}
            ,{field: 'supplierName', title: '供应商名称',sort: true}//当field是直属属性时,可以不用temple去获取!
            ,{field: 'userName', title: '采购员', sort: true}
            ,{field: 'purchaseDate', title: '采购时间',sort: true,templet:function(resp){
                return dateArrayTransfer(resp.purchaseDate,'yyyy-MM-dd HH:mm:ss');
                }}
            ,{fixed:'right',title:'操作',toolbar:'#operatBtn'}



function dateArrayTransfer(dateArray) {
    if(dateArray == null || dateArray == ''){
        return '';
    }
    var returnDate = dateArray[0]+"-"+
        returnDoubleNum(dateArray[1])+"-"+
        returnDoubleNum(dateArray[2])+" "+
        returnDoubleNum(dateArray[3])+":"+
        returnDoubleNum(dateArray[4])+":"+
        returnDoubleNum(dateArray[5]);
    return returnDate;
}
//保证两位数
function returnDoubleNum(number) {
    return (Array(2).join(0) + number).slice(-2);//创建一个长度为2的数组,且默认用0填充;然后用传过来的数添加都右边,然后从右向左截取两位!
}
2.后端处理:

两个方法

一、一劳永逸法:修改消息转换器

在WebMvcConfig配置类中扩展Spring Mvc的消息转换器,在此消息转换器中使用提供的对象转换器进行Java对象到json数据的转换():

写在下面这个配置类里,继承了WebMvcConfiguer

package com.aqiuo.config;

import com.aqiuo.Interceptor.LoginInterceptor;
import com.aqiuo.Interceptor.RefreshInterceptor;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

/**
 * 配置访问拦截器
 */
@Configuration
public class MVCConfig implements WebMvcConfigurer {

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor(stringRedisTemplate)).addPathPatterns("/class").addPathPatterns("/course").excludePathPatterns("/user/login").excludePathPatterns("/user/register");
        registry.addInterceptor(new RefreshInterceptor(stringRedisTemplate)).addPathPatterns("/**");
    }

        @Override
        public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
            MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
            ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
            // 不显示为null的字段
            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            SimpleModule simpleModule = new SimpleModule();
            simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
            simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
            objectMapper.registerModule(simpleModule);
            objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            jackson2HttpMessageConverter.setObjectMapper(objectMapper);
            // 放到第一个
            converters.add(0, jackson2HttpMessageConverter);

    }


}

二、简单,增加注解

  1. 在定义LocalDateTime类型的属性上添加两行注解
    @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")                    // 表示返回时间类型
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")      // 表示接收时间类型
    @ApiModelProperty(value = "注册时间")
    private LocalDateTime date;

二者都可以!!!


http://www.kler.cn/news/148321.html

相关文章:

  • 想成为网络安全工程师该如何学习?
  • 记录:如何快捷的从一个对象中取出几个属性组成新的对象
  • HarmonyOS4.0系列——02、汉化插件、声明式开发范式ArkTS和类web开发范式
  • Cesium 展示——地球以及渲染数据导出(下载)为图片或 pdf
  • HarmonyOS简述及开发环境搭建
  • Python入职某新员工大量使用Lambda表达式,却被老员工喷是屎山
  • [Java 源码] 秋招常被问到 GC 相关的几道面试题(集中在分配以及回收)
  • C++:char* array = “Hello World“报错怎么办
  • Redis常用操作及应用(一)
  • C++学习——类和对象(上)
  • IvorySQL3.0:基于PG16.0最新内核,实现兼容Oracle数据库再升级
  • 前端css粘性布局,顶部吸附效果(position: sticky)
  • Lua判断字符串包含另一个字符串
  • python中模块的创建及引用(import as,import,from)
  • IP地址定位的误差问题及解析
  • 212. 单词搜索 II
  • react等效memo的方法
  • vue3安装eslint和prettier,最简单的步骤
  • Appium自动化测试:sdk版本和手机版本不兼容你就这样做,分分钟解决问题
  • 音视频学习(十九)——rtsp收流(tcp方式)
  • django 新建流程
  • Flask Session 登录认证模块
  • Python小技巧:探索函数调用为何加速代码执行
  • 力扣23. 合并 K 个升序链表(java,最小堆解法)
  • vivado产生报告阅读分析27
  • 前端相关免查整合-vue、es、工具类等
  • 计算机丢失vcomp140.dll是什么意思,如何解决与修复(附教程)
  • 代码随想录算法训练营 ---第四十二天
  • ubuntu 使用快照启动polygon主网
  • C语言——数组转换