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);
}
}
}