@JSONField或@JsonProperty注解使用
一、需求
使用@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依赖
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version></dependency>
四、使用@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 {@Beanpublic HttpMessageConverter configureMessageConverters() {FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();FastJsonConfig config = new FastJsonConfig();config.setSerializerFeatures(// 保留map空的字段SerializerFeature.WriteMapNullValue,// 将String类型的null转成""SerializerFeature.WriteNullStringAsEmpty,// 将Number类型的null转成0SerializerFeature.WriteNullNumberAsZero,// 将List类型的null转成[]SerializerFeature.WriteNullListAsEmpty,// 将Boolean类型的null转成falseSerializerFeature.WriteNullBooleanAsFalse,// 避免循环引用SerializerFeature.DisableCircularReferenceDetect);converter.setFastJsonConfig(config);converter.setDefaultCharset(Charset.forName("UTF-8"));List<MediaType> 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;}@Overridepublic 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注解