Java解決LocalDateTime傳輸前端為時(shí)間的數(shù)組
問題出現(xiàn)如下:
問題出現(xiàn)原因:
默認(rèn)序列化情況下會(huì)使用SerializationFeature.WRITE_DATES_AS_TIMESTAMPS。使用這個(gè)解析時(shí)就會(huì)打印出數(shù)組。
解決方法:
我在全文搜索處理方法總結(jié)如下:
1.前端自定義函數(shù)來書寫
,cols: [[ //表頭 {type: 'checkbox', fixed: 'left'} ,{field: 'purchaseId', title: 'ID', sort: true, fixed: 'left',hide:true} ,{field: 'supplierName', title: '供應(yīng)商名稱',sort: true}//當(dāng)field是直屬屬性時(shí),可以不用temple去獲??! ,{field: 'userName', title: '采購員', sort: true} ,{field: 'purchaseDate', title: '采購時(shí)間',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; } //保證兩位數(shù) function returnDoubleNum(number) { return (Array(2).join(0) + number).slice(-2);//創(chuàng)建一個(gè)長度為2的數(shù)組,且默認(rèn)用0填充;然后用傳過來的數(shù)添加都右邊,然后從右向左截取兩位! }
2.后端處理:
兩個(gè)方法
一、一勞永逸法:修改消息轉(zhuǎn)換器
在WebMvcConfig配置類中擴(kuò)展Spring Mvc的消息轉(zhuǎn)換器,在此消息轉(zhuǎn)換器中使用提供的對象轉(zhuǎn)換器進(jìn)行Java對象到j(luò)son數(shù)據(jù)的轉(zhuǎn)換():
寫在下面這個(gè)配置類里,繼承了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); // 放到第一個(gè) converters.add(0, jackson2HttpMessageConverter); } }
二、簡單,增加注解
- 在定義LocalDateTime類型的屬性上添加兩行注解
@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss") // 表示返回時(shí)間類型 @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") // 表示接收時(shí)間類型 @ApiModelProperty(value = "注冊時(shí)間") private LocalDateTime date;
二者都可以!??!
到此這篇關(guān)于Java解決LocalDateTime傳輸前端為時(shí)間的數(shù)組的文章就介紹到這了,更多相關(guān)Java LocalDateTime傳輸時(shí)間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Java和PostgreSQL存儲(chǔ)向量數(shù)據(jù)的實(shí)現(xiàn)指南
在當(dāng)今的數(shù)字化時(shí)代,數(shù)據(jù)存儲(chǔ)的方式和技術(shù)正變得越來越復(fù)雜和多樣化,隨著機(jī)器學(xué)習(xí)和數(shù)據(jù)科學(xué)的發(fā)展,向量數(shù)據(jù)的存儲(chǔ)和管理變得尤為重要,本文將詳細(xì)介紹如何使用 Java 和 PostgreSQL 數(shù)據(jù)庫來存儲(chǔ)向量數(shù)據(jù),需要的朋友可以參考下2024-09-09IDEA 集成log4j將SQL語句打印在控制臺(tái)上的實(shí)現(xiàn)操作
這篇文章主要介紹了IDEA 集成log4j將SQL語句打印在控制臺(tái)上的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02springboot調(diào)用支付寶第三方接口(沙箱環(huán)境)
這篇文章主要介紹了springboot+調(diào)用支付寶第三方接口(沙箱環(huán)境),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Java中的CAS鎖機(jī)制(無鎖、自旋鎖、樂觀鎖、輕量級鎖)詳解
這篇文章主要介紹了Java中的CAS鎖機(jī)制(無鎖、自旋鎖、樂觀鎖、輕量級鎖)詳解,CAS算法的作用是解決多線程條件下使用鎖造成性能損耗問題的算法,保證了原子性,這個(gè)原子操作是由CPU來完成的,需要的朋友可以參考下2024-01-01Spring?Boot?Security認(rèn)證之Redis緩存用戶信息詳解
本文介紹了如何使用Spring Boot Security進(jìn)行認(rèn)證,并通過Redis緩存用戶信息以提高系統(tǒng)性能,通過配置RedisUserDetailsManager,我們成功地將用戶信息存儲(chǔ)到了Redis中,并在Spring Security中進(jìn)行了集成,需要的朋友可以參考下2024-01-01使用Idea快速搭建SpringMVC項(xiàng)目的詳細(xì)步驟記錄
這篇文章主要給大家介紹了關(guān)于使用Idea快速搭建SpringMVC項(xiàng)目的詳細(xì)步驟,Spring?MVC是一種基于MVC模式的框架,它是Spring框架的一部分,它提供了一種更簡單和更有效的方式來構(gòu)建Web應(yīng)用程序,需要的朋友可以參考下2024-05-05