在SpringBoot中的Jackson使用笔记
常用的java转json,json反序列化为java等方法,这里定义成一个工具类来用jackson
package cn.ath.knowwikibackend.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
@Slf4j
public class JacksonUtil {
private JacksonUtil() {
}
// 日期格式化
private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 配置正常的驼峰字段形式的json用的 OBJECT_MAPPER
* {
* "name":"tom",
* "age: 22,
* "serialNumber":"STU0213131"
* }
*/
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
static {
//对象的所有字段全部列入
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
//取消默认转换timestamps形式
OBJECT_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//忽略空Bean转json的错误
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
OBJECT_MAPPER.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
//忽略 在json字符串中存在,但是在java对象中不存在对应属性的情况。防止错误
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
/**
* 配置 下划线字段形式的json用的 OBJECT_MAPPER
* {
* "name":"tom",
* "age: 22,
* "serial_number":"STU0213131"
* }
*/
private static final ObjectMapper OBJECT_MAPPER_SNAKE_CASE = new ObjectMapper();
static {
//对象的所有字段全部列入
OBJECT_MAPPER_SNAKE_CASE.setSerializationInclusion(JsonInclude.Include.ALWAYS);
//取消默认转换timestamps形式
OBJECT_MAPPER_SNAKE_CASE.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//忽略空Bean转json的错误
OBJECT_MAPPER_SNAKE_CASE.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
OBJECT_MAPPER_SNAKE_CASE.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
//忽略 在json字符串中存在,但是在java对象中不存在对应属性的情况。防止错误
OBJECT_MAPPER_SNAKE_CASE.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//转换为下划线
OBJECT_MAPPER_SNAKE_CASE.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
}
/**
* 配置属性名不带引号的 非标准格式 的json 用的 OBJECT_MAPPER
*
* Neo4j场景会用到这种格式
* {
* name:"tom",
* age: 22,
* serialNumber:"STU0213131"
* }
*
* 如这个cql语句
* merge(:学生 {name:"tom",age: 22,serialNumber:"STU0213131"})
*/
private static final ObjectMapper OBJECT_MAPPER_BY_QUOTE_FIELD_NAMES = new ObjectMapper();
static {
OBJECT_MAPPER_BY_QUOTE_FIELD_NAMES
.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
}
/**
* map转换成json格式String
* @param map Map<String,Object>
* @return String
*/
public static String map2String(Map<String,Object> map){
String propertiesString = "";
try {
propertiesString = OBJECT_MAPPER_BY_QUOTE_FIELD_NAMES.writeValueAsString(map);
} catch (JsonProcessingException e) {
log.error(e.getMessage());
}
return propertiesString;
}
/**
* 对象转Json格式字符串
*
* @param obj 对象
* @return Json格式字符串
*/
public static <T> String obj2String(T obj) {
if (obj == null) {
return null;
}
try {
return obj instanceof String ? (String) obj : OBJECT_MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.warn("Parse Object to String error : {}", e.getMessage());
return null;
}
}
/**
* 对象转file
* @param fileName String
* @param obj Object
*/
public static void obj2File(String fileName,Object obj){
if (obj == null){
return;
}
try {
OBJECT_MAPPER.writeValue(new File(fileName),obj);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 对象转Json格式字符串(格式化的Json字符串)
*
* @param obj 对象
* @return 美化的Json格式字符串
*/
public static <T> String obj2StringPretty(T obj) {
if (obj == null) {
return null;
}
try {
return obj instanceof String ? (String) obj : OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.warn("Parse Object to String error : {}", e.getMessage());
return null;
}
}
/**
* 字符串转换为自定义对象
*
* @param str 要转换的字符串
* @param clazz 自定义对象的class对象
* @return 自定义对象
*/
public static <T> T string2Obj(String str, Class<T> clazz) {
if (StringUtils.isEmpty(str) || clazz == null) {
return null;
}
try {
return clazz.equals(String.class) ? (T) str : OBJECT_MAPPER.readValue(str, clazz);
} catch (Exception e) {
log.warn("Parse String to Object error : {}", e.getMessage());
return null;
}
}
/**
* 对象转Json格式字符串; 属性名从驼峰改为下划线形式
*
* @param obj 对象
* @return Json格式字符串
*/
public static <T> String obj2StringFieldSnakeCase(T obj) {
if (obj == null) {
return null;
}
try {
ObjectMapper objectMapper = OBJECT_MAPPER_SNAKE_CASE;
return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.warn("Parse Object to String error : {}", e.getMessage());
return null;
}
}
/**
* 字符串转换为自定义对象; 属性名从下划线形式改为驼峰
*
* @param str 要转换的字符串
* @param clazz 自定义对象的class对象
* @return 自定义对象
*/
public static <T> T string2ObjFieldLowerCamelCase(String str, Class<T> clazz) {
if (StringUtils.isEmpty(str) || clazz == null) {
return null;
}
try {
ObjectMapper objectMapper = OBJECT_MAPPER_SNAKE_CASE;
return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str, clazz);
} catch (Exception e) {
log.warn("Parse String to Object error : {}", e.getMessage());
return null;
}
}
/**
* 字符串转换为自定义对象(List); 属性名从下划线形式改为驼峰
*
* @param str 要转换的字符串
* @param typeReference 自定义对象的typeReference List 对象
* @return 自定义对象
*/
public static <T> List<T> string2ListFieldLowerCamelCase(String str, TypeReference<List<T>> typeReference) {
if (StringUtils.isEmpty(str) || typeReference == null) {
return null;
}
try {
ObjectMapper objectMapper = OBJECT_MAPPER_SNAKE_CASE;
return objectMapper.readValue(str, typeReference);
} catch (Exception e) {
log.warn("Parse String to Object error : {}", e.getMessage());
return null;
}
}
/**
* 字符串转换为自定义字段转为list
* @param str String
* @param typeReference TypeReference
* @param <T> 泛型
* @return T 类型
*/
public static <T> T string2Obj(String str, TypeReference<T> typeReference) {
if (StringUtils.isEmpty(str) || typeReference == null) {
return null;
}
try {
return (T) (typeReference.getType().equals(String.class) ? str : OBJECT_MAPPER.readValue(str, typeReference));
} catch (IOException e) {
log.warn("Parse String to Object error", e);
return null;
}
}
public static <T> T string2Obj(String str, Class<?> collectionClazz, Class<?>... elementClazzes) {
JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
try {
return OBJECT_MAPPER.readValue(str, javaType);
} catch (IOException e) {
log.warn("Parse String to Object error : {}" + e.getMessage());
return null;
}
}
}
在springboot项目中配置全局序列化date返回前端字符串
需要在yml里配置jsckson
spring:
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss