引物在线设计网站,营销型网站建设有哪些,多个链接的网站怎么做的,海口网约车平台一、需求
使用JSONField或JsonProperty注解#xff0c;来解决bean与json字段不一致问题#xff0c;或者字段定义不符合前端所需要的标准#xff0c;最近在项目中发现实体类属性中#xff0c;同时使用了JSONField和JsonProperty注解#xff0c;用于重新声明属性key。有时候…一、需求
使用JSONField或JsonProperty注解来解决bean与json字段不一致问题或者字段定义不符合前端所需要的标准最近在项目中发现实体类属性中同时使用了JSONField和JsonProperty注解用于重新声明属性key。有时候注解生效有时候注解不生效。我很好奇到底是哪个注解生效了于是进行了各种验证基本搞明白了。
二、JsonProperty和JSONField注解的区别
1.底层框架不同
JsonProperty 是Jackson实现的 JSONField 是fastjson实现的
2.用法不同
1bean序列化为Json
JsonPropertyObjectMapper().writeValueAsString(Object value)
JSONFieldObjectMapper().readValue(String content, Class valueType)
2Json反序列化为bean
JsonPropertyObjectMapper().readValue(String content, Class valueType)
JSONFieldJSONObject.parseObject(String content, Class valueType)
3作用域
JSONproperty 注解用于属性上面 如把trueName属性序列化为name可以在属性名上面增加JsonProperty(value“name”)。
JSONField 注解可以用于get、set以及属性上面 如把trueName属性序列化为name可以在get/set/属性名上面增加JSONField(value“name”)。
三、pom依赖 dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.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));ListMediaType mediaTypeList new ArrayList();// 解决中文乱码问题相当于在Controller上的RequestMapping中加了个属性produces application/jsonmediaTypeList.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(namemy_name)private String name;private String phone;JSONField(namemy_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注解