fastjson转换对象实体@JsonProperty不生效问题及解决
作者:码农晴明_ 时间:2023-10-07 00:13:51
fastjson转换对象实体@JsonProperty不生效
项目场景
请求第三方应用 返回json数据
问题描述
第三方返回的数据中,存在java关键词,无法直接使用原属性名进行对应 例如(class、interface等)使用@JsonProperty注解不能返回正确的结果
@Data
static class User{
@JsonProperty( "class")
private String userClass;
@JsonProperty("interface")
private String userInterface;
}
public static void main(String[] args) {
Map<String,Object> map = new HashMap<>();
map.put("class","测试");
map.put("interface","测试1");
String mapStr = JSONObject.toJSONString(map);
System.out.println(mapStr);
User user = JSONObject.parseObject(mapStr, User.class);
System.out.println(user);
}
正常情况来讲 @JsonProperty 注解完全够用,可以成功解析出想要的结果。
但往往事情并不是那么简单
执行结果 :
{"interface":"测试1","class":"测试"}
User(userClass=null, userInterface=null)
可以看出并没有成功映射到想要的数据
原因分析
具体原因感兴趣的同学可以看下 JSONObject.parseObject 的源码
解决方案
解决方法有两种
1、修改属性名称,使用原属性名 + “_”
@Data
static class User{
@JsonProperty( "class")
private String class_;
@JsonProperty("interface")
private String interface_;
}
public static void main(String[] args) {
Map<String,Object> map = new HashMap<>();
map.put("class","测试");
map.put("interface","测试1");
String mapStr = JSONObject.toJSONString(map);
System.out.println(mapStr);
User user = JSONObject.parseObject(mapStr, User.class);
System.out.println(user);
}
执行结果 :
{"interface":"测试1","class":"测试"}
User(class_=测试, interface_=测试1)
2、使用fastjson @JSONField注解
@Data
static class User{
@JSONField(name = "class")
private String userClass;
@JSONField(name = "interface")
private String userInterface;
}
public static void main(String[] args) {
Map<String,Object> map = new HashMap<>();
map.put("class","测试");
map.put("interface","测试1");
String mapStr = JSONObject.toJSONString(map);
System.out.println(mapStr);
User user = JSONObject.parseObject(mapStr, User.class);
System.out.println(user);
}
执行结果:
{"interface":"测试1","class":"测试"}
User(userClass=测试, userInterface=测试1)
@JsonProperty 失效问题的排查
@JsonProperty 是Jackson提供的一个用于注解属性、类、方法等的json注解。使用它可以改变Json序列化时属性的名称,一般默认使用属性名,比如如下的代码示例,如果没有使用@JsonProperty注解那么id转化为json为{“id”:11}.使用了则就是{“Id”:11}.
@JsonInclude(Include.NON_NULL)
public class User implements Serializable {
@JsonProperty("Id")
private Integer id;
@JsonProperty("Name")
private String name;
@JsonProperty("pwd")
private Integer passWord;
}
在一次使用springboot项目时发现@JsonProperty不生效。
那么是因为啥呢?
因为在项目里还引用了fastJson,在debug时发现接口最后响应时是使用FastJson做json序列化。
解决方法:
使用@EnableWebMvc注解,加在启动类上。或者直接在项目里不引用fastJson.
@EnableWebMvc
public class SpringBootMain extends SpringBootServletInitializer implements WebApplicationInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootMain.class);
}
}
springboot 是如何选择使用json序列化工具的呢?即如何调用jackson进行json序列化和反序列化?
springboot 通过HttpMessageConverters 消息转换器通过jackson将java对象转化为json字符串。如果项目里包含多个json工具包比如jackson ,fastjson,那么就会各个年级对象的内容选择一个合适的去转换为json。
这是HttpMessageConverters 消息转换器所处的位置,所以项目里采用那个json工具由该类决定。
springboot默认使用jackson,springboot默认集成的就是jackson。
指定使用fastJson的一种做法:
public class SpringBootMain extends SpringBootServletInitializer implements WebApplicationInitializer {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
}
}
来源:https://blog.csdn.net/smlie_shuihan/article/details/121420204
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android中自定义View的实现方式总结大全
Springmvc ResponseBody响应json数据实现过程
C#定时关闭窗体实例
Android可配置透明度的水印
![](https://img.aspxhome.com/file/2023/1/89551_0s.png)
C# dll代码混淆加密的实现
![](https://img.aspxhome.com/file/2023/5/91265_0s.jpg)
Unity Shader实现3D翻页效果
![](https://img.aspxhome.com/file/2023/8/106368_0s.gif)
Android扩大View点击范围的方法
![](https://img.aspxhome.com/file/2023/1/137811_0s.jpg)
C#利用SharpPcap实现网络包捕获嗅探
![](https://img.aspxhome.com/file/2023/1/122721_0s.png)
Android动画之小球拟合动画实例
![](https://img.aspxhome.com/file/2023/0/138090_0s.gif)
Unity 如何通过反射给gameObject添加组件
android中DatePicker和TimePicker的使用方法详解
![](https://img.aspxhome.com/file/2023/3/106043_0s.png)
SpringBoot 如何优雅的实现跨服务器上传文件的示例
![](https://img.aspxhome.com/file/2023/9/83039_0s.png)
Springboot集成ClickHouse及应用场景分析
![](https://img.aspxhome.com/file/2023/7/81937_0s.gif)
Spring Security OAuth2 实现登录互踢的示例代码
![](https://img.aspxhome.com/file/2023/7/58307_0s.jpg)
Java实现带GUI的气泡诗词效果
服务器端C#实现的CSS解析器
一款域名监控小工具 Domain(IP)Watcher 实现代码
![](https://img.aspxhome.com/file/2023/6/98256_0s.jpg)
SpringCloud迈向云原生的步骤
![](https://img.aspxhome.com/file/2023/2/95032_0s.png)
Springboot JPA 枚举Enum类型存入到数据库的操作
![](https://img.aspxhome.com/file/2023/4/60064_0s.jpg)