jackson 通用泛型解决方案

maven 坐标

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.1</version>
</dependency>

在我们做业务开发的时候经常对于http请求封装一个公用的类,如下:

public class HttpResponse<DATA>{
	private long code;
	private boolean success;
	private String message;
	private DATA data;
}

在后端使用fastjson解析泛型的时候会让人头皮发麻,所以废话不多说…上代码:

package com.wisdom.imd.workplatform.client.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class WsdJacksonUtils {

    public static ObjectMapper defaultMapper() {
        ObjectMapper mapper = new ObjectMapper();
        //在反序列化时忽略在 json 中存在但 Java 对象不存在的属性
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //在序列化时日期格式默认为 yyyy-MM-dd'T'HH:mm:ss.SSSZ
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        //在序列化时自定义时间日期格式
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        //在序列化时忽略值为 null 的属性
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //在序列化时忽略值为默认值的属性
        mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_DEFAULT);
        return mapper;
    }

    public static <T> T decode(String jsonString, Class<T> clazz) {
        try {
            return defaultMapper().readValue(jsonString, clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 多重泛型  数据结构
     * List<Outer<INNER>>
     *
     * @return
     */
    public static <OUTER, INNER> List<OUTER> decodeRawList(String jsonString, Class<OUTER> outerClass, Class<INNER> innerClass) {
        ObjectMapper mapper = defaultMapper();
        JavaType javaType = mapper.getTypeFactory().constructParametricType(outerClass, innerClass);
        javaType = mapper.getTypeFactory().constructCollectionType(List.class, javaType);
        try {
            return mapper.readValue(jsonString, javaType);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 多重泛型 数据结构 OUTER<List<Inner>>
     *
     * @return
     */
    public static <OUTER, INNER> OUTER decodeRawMidList(String jsonString, Class<OUTER> outerClass, Class<INNER> innerClass) {
        ObjectMapper mapper = defaultMapper();
        JavaType javaType = mapper.getTypeFactory().constructCollectionType(List.class, innerClass);
        javaType = mapper.getTypeFactory().constructParametricType(outerClass, javaType);
        try {
            return mapper.readValue(jsonString, javaType);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static <OUTER, INNER> OUTER decodeRaw(String jsonString, Class<OUTER> outerClass, Class<INNER> innerClass) {
        ObjectMapper mapper = defaultMapper();
        JavaType javaType = mapper.getTypeFactory().constructParametricType(outerClass, innerClass);
        try {
            return mapper.readValue(jsonString, javaType);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static <OUTER extends Collection<INNER>, INNER> OUTER decodeToCollection(String jsonString, Class<OUTER> outerClass, Class<INNER> innerClass) {
        ObjectMapper mapper = defaultMapper();
        JavaType javaType = mapper.getTypeFactory().constructCollectionType(outerClass, innerClass);
        try {
            return defaultMapper().readValue(jsonString, javaType);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static <T> List<T> decodeToList(String jsonString, Class<T> innerClass) {
        return (List<T>) decodeToCollection(jsonString, ArrayList.class, innerClass);
    }

    public static String toJsonString(Object obj) {
        try {
            return defaultMapper().writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值