問題出現如下:
問題出現原因:
默認序列化情況下會使用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 {@AutowiredStringRedisTemplate stringRedisTemplate;@Overridepublic 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("/**");}@Overridepublic 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);}}
二、簡單,增加注解
- 在定義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;
二者都可以!!!