不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2021-12-19 19:58:47  修改时间:2024-04-14 15:38:25  分类:Java框架/系统  编辑

【Spring Boot】Json工具库Jackson,全局配置失效

1、全局配置

配置在application.yml文件中

spring:
  jackson:
    #日期格式化
    date-format: yyyy-MM-dd HH:mm:ss
    serialization:
      #格式化输出 
      indent_output: true
      #忽略无法转换的对象
      fail_on_empty_beans: false
    #设置空如何序列化
    defaultPropertyInclusion: NON_NULL # 空值字段不传输(NON_EMPTY)
    deserialization:
      #允许对象忽略json中不存在的属性
      fail_on_unknown_properties: false
    parser:
      #允许出现特殊字符和转义符
      allow_unquoted_control_chars: true
      #允许出现单引号
      allow_single_quotes: true

2、自定义字段/属性名:@JsonProperty

@ApiModel(value = "User对象", description = "用户表")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    // 自定义Json字段名
    @JsonProperty("userId")
    @ApiModelProperty("自增ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty("手机号")
    private String phone;
	
	// 其他字段省略......
}

3、忽略某个字段/属性:@JsonIgnore

@ApiModel(value = "User对象", description = "用户表")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    // 自定义Json字段名
    @JsonProperty("userId")
    @ApiModelProperty("自增ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty("手机号")
    private String phone;
	
	// 忽略Json输出
	@JsonIgnore
    @ApiModelProperty("登录密码")
    private String password;
	
	// 其他字段省略......
}

4、Json组手类

package wen.jianbao.common.helper;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Json助手类
 */
public class JsonHelper {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 对象转字符串
     */
    public static String objectToString(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 字符串转对象
     */
    public static <T> T stringToObject(String json, Class<T> object) {
        try {
            return objectMapper.readValue(json, object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 转JSON对象
     */
    public static JSONArray parseArray(Object arrayOrCollection) {
        JSONConfig config = JSONConfig.create().setIgnoreCase(false)
            .setOrder(true).setDateFormat("yyyy-MM-dd HH:mm:ss");
        return JSONUtil.parseArray(arrayOrCollection, config);
    }

    /**
     * 转JSON数组对象
     */
    public static JSONObject parseObject(Object obj) {
        JSONConfig config = JSONConfig.create().setIgnoreCase(false)
            .setOrder(true).setDateFormat("yyyy-MM-dd HH:mm:ss");
        return JSONUtil.parseObj(obj, config);
    }
}