@JSONField或@JsonProperty注解使用
创始人
2024-12-01 08:36:26
0

一、需求

使用@JSONField或@JsonProperty注解,来解决bean与json字段不一致问题,或者字段定义不符合前端所需要的标准,最近在项目中发现实体类属性中,同时使用了@JSONField和@JsonProperty注解,用于重新声明属性key。有时候注解生效,有时候注解不生效。我很好奇到底是哪个注解生效了,于是进行了各种验证,基本搞明白了。

二、@JsonProperty和@JSONField注解的区别

1.底层框架不同

@JsonProperty 是Jackson实现的
@JSONField 是fastjson实现的

2.用法不同

(1)bean序列化为Json:

@JsonProperty:ObjectMapper().writeValueAsString(Object value)

@JSONField:ObjectMapper().readValue(String content, Class valueType)

(2)Json反序列化为bean:

@JsonProperty:ObjectMapper().readValue(String content, Class valueType)

@JSONField:JSONObject.parseObject(String content, Class valueType)

(3)作用域

@JSONproperty 注解用于属性上面
如把trueName属性序列化为name,可以在属性名上面增加@JsonProperty(value=“name”)。

@JSONField 注解可以用于get、set以及属性上面
如把trueName属性序列化为name,可以在get/set/属性名上面增加@JSONField(value=“name”)。

三、pom依赖

                      com.alibaba             fastjson             1.2.83          

四、使用@JsonProperty

如果使用的是新建的springboot项目,默认就是Jackson序列化,直接在属性上使用注解即可。

五、使用@JSONField

必须重写数据解析器,使用fastjson进行序列化和反序列化。配置类如下:

import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter;  import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List;  /**  * @Author:   * @Description  * @Date: 下午1:59 2023/11/9  */ @Configuration public class WebMvcConfiguration {      @Bean     public HttpMessageConverter configureMessageConverters() {         FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();         FastJsonConfig config = new FastJsonConfig();         config.setSerializerFeatures(                 // 保留map空的字段                 SerializerFeature.WriteMapNullValue,                 // 将String类型的null转成""                 SerializerFeature.WriteNullStringAsEmpty,                 // 将Number类型的null转成0                 SerializerFeature.WriteNullNumberAsZero,                 // 将List类型的null转成[]                 SerializerFeature.WriteNullListAsEmpty,                 // 将Boolean类型的null转成false                 SerializerFeature.WriteNullBooleanAsFalse,                 // 避免循环引用                 SerializerFeature.DisableCircularReferenceDetect);          converter.setFastJsonConfig(config);         converter.setDefaultCharset(Charset.forName("UTF-8"));         List mediaTypeList = new ArrayList<>();         // 解决中文乱码问题,相当于在Controller上的@RequestMapping中加了个属性produces = "application/json"         mediaTypeList.add(MediaType.APPLICATION_JSON);         converter.setSupportedMediaTypes(mediaTypeList);         return converter;     } } 

六、实体类

import com.alibaba.fastjson.annotation.JSONField; /**  * @Author:   * @Description  * @Date: 上午10:36 2023/11/9  */ public class JSONFieldData {     @JSONField(name="my_name")     private String name;     private String phone;     @JSONField(name="my_age")     private Integer age;      public JSONFieldData() {     }      public JSONFieldData(String name, String phone, Integer age) {         this.name = name;         this.phone = phone;         this.age = age;     }       public void setAge(Integer age) {         this.age = age;     }      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }      public String getPhone() {         return phone;     }      public void setPhone(String phone) {         this.phone = phone;     }      public Integer getAge() {         return age;     }      @Override     public String toString() {         return "JSONFieldData{" +                 "name='" + name + '\'' +                 ", phone='" + phone + '\'' +                 ", age=" + age +                 '}';     } } 

七、定义controller

import org.springframework.web.bind.annotation.*;  /**  * @Author:   * @Description  * @Date: 上午10:42 2023/11/9  */ @RestController @RequestMapping("/json") public class JSONFieldApi {      @GetMapping("/test1")     public Object testJson() {         JSONFieldData data = new JSONFieldData();         data.setName("cjAqNP");         data.setPhone("bdfuNn");         data.setAge(399);         return data;     }      @PostMapping("/test2")     public Object testJson2(@RequestBody JSONFieldData data) {         return data;     }  } 

八、测试@JSONField注解

在这里插入图片描述
在这里插入图片描述

相关内容

热门资讯

金花游戏房卡怎么获得最简单方法... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
秒懂教程“牛牛链接房卡那里有/... 先锋大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
微信金花房卡链接如何购买/购买... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
微信金花群怎么买房卡/购买斗牛... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
房卡必备教程“链接金花房卡怎么... 新星游牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
正版金花房卡哪里有卖/金花链接... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
一分钟了解“炸金花房卡专卖店联... 新西游牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
微信链接牛牛群房卡怎么买/微信... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
一分钟推荐“金花大厅房卡链接在... 毛豆大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
终于找到“购买斗牛金花房卡联系... 卡米大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
一分钟推荐“金花房卡在哪里能弄... 新超圣牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
微信群链接牛牛买房卡/微信牛牛... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
牛牛金花房卡链接在哪买/金花客... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
金花大厅房卡如何购买的/微信金... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
微信里面炸金花房卡充值方式/在... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
终于找到“微信链接斗牛房卡开科... 微信网页牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房...
终于找到“金花房卡微信充值/毛... 毛豆大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
牛牛链接房卡在哪里弄/炸金花房... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
一分钟了解“金花斗牛牛房卡怎么... 悠悠互娱是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
正版授权“微信怎么创建金花房间... 毛豆大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来...