解决Json传输LocalDateTime时JackSon解析出现LocalDateTime的序列化问题

JackSon版本信息


        2.10.0


        
            com.fasterxml.jackson.datatype
            jackson-datatype-jsr310
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            ${jackson.version}
        

JackSon的工具类

public final class JsonUtils {

    private JsonUtils(){}
    /**
     * 将对象转换成JSON字符串
     * @param obj 对象
     * @return json字符串
     */
    public static String objectToJsonString(Object obj) throws JsonParseException {
        try {
            ObjectMapper mapper = new ObjectMapper();
            JavaTimeModule timeModule = new JavaTimeModule();
            timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
            mapper.registerModule(timeModule);
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new JsonParseException("Json解析错误。");
        }
    }

    /**
     * 将JSON字符串转换成对象
     * @param json json字符串
     * @param clazz 指定的类
     *       

* 如果实体类中包含时间类别的字段,clazz不允许出现集合类型。 * 如果哟啊获取结合,请使用getList()方法 *

* @return 指定的类型 */ public static T jsonStringToObject(String json, Class clazz) throws JsonParseException{ try { ObjectMapper mapper = new ObjectMapper(); JavaTimeModule timeModule = new JavaTimeModule(); timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); mapper.registerModule(timeModule); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); return mapper.readValue(json,clazz); } catch (JsonProcessingException e) { throw new JsonParseException("Json解析错误。"); } } /** * 获取List集合 * @param json json字符串 * @param t 实体类 * @return 集合 * @throws JsonParseException 解析异常 */ public static List getList(String json, Class t) throws JsonParseException { try { List result = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); JavaTimeModule timeModule = new JavaTimeModule(); timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); mapper.registerModule(timeModule); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); List list = mapper.readValue(json, List.class); for (int i = 0; i < list.size(); i++) { String jsonString = objectToJsonString(list.get(i)); result.add(jsonStringToObject(jsonString,t)); } return result; } catch (JsonProcessingException e) { throw new JsonParseException("Json解析错误。"); } } /** * 解析Json字符串,并获取某一个节点的值 * @param json json字符串 * @param nodeName 节点名 * @return 该节点的值 * @throws JsonParseException 解析异常 */ public static String parseJson(String json,String nodeName) throws JsonParseException { ObjectMapper mapper = new ObjectMapper(); try { JsonNode node = mapper.readTree(json); JsonNode value = node.findValue(nodeName); return value.toString(); } catch (JsonProcessingException e) { throw new JsonParseException("Json解析错误"); } } }

自定义序列化与反序列化

/**
 * LocalDateTime序列化
 * @author xiaoyang
 * @date 2020/06/12
 */
public final class LocalDateTimeSerializer extends JsonSerializer {
    @Override
    public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeNumber(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
    }
}

/**
 * LocalDateTime反序列化
 * @author xiaoyang
 * @date 2020/06/12
 */
public final class LocalDateTimeDeserializer extends JsonDeserializer {
    @Override
    public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        long timestamp = jsonParser.getLongValue();
        return LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8));
    }
}

你可能感兴趣的:(Java,开发,拦截)