解决方案:使用 jackson
1.创建utils类
package com.mybatis.plus.utils; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; @Slf4j public class JsonUtils { private static ObjectMapper mapper = new ObjectMapper(); static { // 对于空的对象转json的时候不抛出错误 mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); // 允许属性名称没有引号 mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); // 允许单引号 mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); // 设置输入时忽略在json字符串中存在但在java对象实际没有的属性 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // 设置输出时包含属性的风格 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); } /** * 序列化,将对象转化为json字符串 * * @param data * @return */ public static String toJsonString(Object data) { if (data == null) { return null; } String json = null; try { json = mapper.writeValueAsString(data); } catch (JsonProcessingException e) { log.error("[{}] toJsonString error:{{}}", data.getClass().getSimpleName(), e); } return json; } /** * 反序列化,将json字符串转化为对象 * * @param json * @param clazz * @param <T> * @return */ public static <T> T parse(@NonNull String json, Class<T> clazz) { T t = null; try { t = mapper.readValue(json, clazz); } catch (Exception e) { log.error(" parse json [{}] to class [{}] error:{{}}", json, clazz.getSimpleName(), e); } return t; } }
调用效果