@JsonProperty和@JSONField注解的区别

JSON(JavaScript Object Notation)

json是一种常见的数据交换的轻量级数据格式。HTTP协议传输数据可以有多种数据格式,比如下面几种常见数据传输格式,除此之外还有其他的数据交换格式。

数据传输类型编码类型示例
表单格式application/x-www-form-urlencodedusername=zk&password=123
JSON(JavaScript Object Notation)application/json{"username": "zk","password": "123"}
XML(eXtensible Markup Language)application/xml<user><username>zk</username><password>123</password></user>

Jackson

Jackson是一款优秀的JSON解析库,添加了依赖之后就可以使用对应的注解,让我们能够自由的将Java对象和JSON做转换。在这里插入图片描述
比如Java对象转JSON
在这里插入图片描述

@JsonProperty和@JSONField

为了解决JSON字符串和其实体bean的属性名匹配不上的问题,@JsonProperty和@JSONField都可以将某一属性名序列化为另一属性名。

那么@JsonProperty和@JSONField有什么区别呢?

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hust.zhang.serializable.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

public class JsonPropertiesVsJsonField {
    @AllArgsConstructor
    @Data
    @Builder
    static class Properties {
        @JsonProperty(value = "json-properties")
        private String jsonProperties;

        @JSONField(name = "json-field")
        private String jsonField;
    }

    public static void main(String[] args) {
        Properties properties = Properties.builder()
                .jsonProperties("test-properties")
                .jsonField("test-field")
                .build();

        System.out.println(JsonUtils.toJson(properties));
        System.out.println(JSON.toJSONString(properties));
    }
}

输出结果如下,

{"jsonField":"test-field","json-properties":"test-properties"}
{"json-field":"test-field","jsonProperties":"test-properties"}

可以看到调用JsonUtils.toJson方法时,加了@JsonProperty才与bean实际属性名匹配。
其中该方法定义如下,ObjectMapper的writeValueAsString方法。

/**
 * Json转换工具类
 */
@Slf4j
public final class JsonUtils {
    /**
     * 私有无参构造方法 常量类不能实例化,直接引用
     */
    private JsonUtils() {

    }

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    static {
        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        OBJECT_MAPPER.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
    }

    public static String toJson(Object object) {
        try {
            return OBJECT_MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            log.error("Failed to write the object to string" + object.getClass().getName());
            return null;
        }
    }

    public static <T>T parse(String json, Class<T> tClass){
        try {
            return OBJECT_MAPPER.readValue(json,tClass);
        } catch (JsonProcessingException e) {
            log.error("Failed to deserialize JSON content, json value : " + json);
            return null;
        }
    }
}

除此之外还可以看看@JsonAlias注解。

参考链接

1、https://baijiahao.baidu.com/s?id=1765042798858921947&wfr=spider&for=pc
2、https://developer.aliyun.com/article/768691

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值