您的位置:首页 > 其它

fastjosn转换成字符串时首字母大小写问题

2017-03-30 14:23 1881 查看
fastjson输出字符串默认首字母小写,如果想要大写(和bean一致),需要使用如下配置。

TypeUtils.compatibleWithJavaBean = true;

之前使用的1.1.*版本,该版本发现注入漏洞,公司统一升级。发现升级为1.2.28 后引入了一个bug。

 

前提:使用了fastjson某些配置。

例:在1.1.*版本中使用如下配置方式,结果正常。

程序在service或controller静态方法中设置的参数。

   static {

       TypeUtils.compatibleWithJavaBean = true;

    }

 

升级后到1.2.28,结果错误。调试时发现TypeUtils.compatibleWithJavaBean在启动时已设置为true,在构造json串时为false。

 

原因:

1.2.28中IOUtils类下面方法会在第一次调用时执行,因此会覆盖在service或controller中的设置。 1.1.*版本无此方法。

static class
PropertiesInitializer {

    PropertiesInitializer() {

    }

    publicvoid autoConfig(){

        IOUtils.loadPropertiesFromFile();

        TypeUtils.compatibleWithJavaBean =
"true".equals(IOUtils.getStringProperty("fastjson.compatibleWithJavaBean"));

        TypeUtils.compatibleWithFieldName =
"true".equals(IOUtils.getStringProperty("fastjson.compatibleWithFieldName"));

    }

}
 

解决办法:

增加配置文件(fastjson.properties),实际内容为“fastjson.compatibleWithJavaBean=true”。

也许还有其他方法,旺告知。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: