java的autotype,关于 fastjson 异常 autoType is not support 问题分析解决
时间: 2024-02-21 09:01:45 浏览: 26
在 FastJson 中,autoType 是指自动类型转换,通常用于将 JSON 字符串转换为 Java 对象。然而,在 FastJson 的默认配置下,autoType 功能是被禁用的,因为它存在安全风险,可能会导致恶意代码注入。因此,如果你在使用 FastJson 时启用了 autoType 功能,就会抛出 "com.alibaba.fastjson.JSONException: autoType is not support" 异常。
要解决这个问题,有以下两种方式:
1. 使用 FastJson 的 setAutoTypeSupport 方法来启用 autoType 功能。但是要注意,启用 autoType 功能后,需要做好安全措施,避免恶意代码注入。
示例代码:
```
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
```
2. 禁用 autoType 功能,采用手动反序列化的方式将 JSON 字符串转换为 Java 对象。
示例代码:
```
JSONObject jsonObject = JSON.parseObject(jsonString);
MyObject myObject = new MyObject();
myObject.setName(jsonObject.getString("name"));
myObject.setAge(jsonObject.getIntValue("age"));
```
总的来说,建议避免使用 FastJson 的 autoType 功能,采用手动反序列化的方式更加安全可靠。如果必须使用 autoType 功能,要做好安全措施,避免恶意代码注入。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)