1.LocalDateTime 对象如何序列化成yyyy-MM-dd HH:mm:ss
使用jackon对LocalDateTime对象进行序列化时
会变成这样的格式
{ "year": 2020, "month": "AUGUST", "era": "CE", "dayOfMonth": 1, "dayOfWeek": "TUESDAY", "dayOfYear": 213, "leapYear": false, "monthValue": 8, "chronology": { "id":"ISO", "calendarType":"iso8601" } }
如何将他变成 2020-01-02 12:12:12
这样的形式呢
需要这么做
ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
就是激活JavaTimeModule
添加依赖
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency>
2.序列化包含泛型的java对象
假如序列化的对象包含泛型,那么可以使用TypeReference
import com.fasterxml.jackson.core.type.TypeReference; DataDto<List<VibrationData>> o = mapper.readValue(json, new TypeReference<DataDto<List<VibrationData>>>() {});
DateDto:
package com.tonglei.recevce.entity.dto; public class DataDto<T> { private T data; private int len; private String instrument; public DataDto(){} public DataDto(T data, int len, String instrument) { this.data = data; this.len = len; this.instrument = instrument; } public T getData() { return data; } public void setData(T data) { this.data = data; } public int getLen() { return len; } public void setLen(int len) { this.len = len; } public String getInstrument() { return instrument; } public void setInstrument(String instrument) { this.instrument = instrument; } }