问题
Bean Shell脚本:
vars.put("udx_flinksql_data",JSON.toJSONString(udx_flinksql_data, SerializerFeature.DisableCircularReferenceDetect));
}
错误信息:
2020-08-17 15:20:43,936 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONArray; i . . . '' : Error in method invocation: Static method toJSONString( com.alibaba.fastjson.JSONObject, com.alibaba.fastjson.serializer.SerializerFeature ) not found in class'com.alibaba.fastjson.JSON'
2020-08-17 15:20:43,936 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONArray; i . . . '' : Error in method invocation: Static method toJSONString( com.alibaba.fastjson.JSONObject, com.alibaba.fastjson.serializer.SerializerFeature ) not found in class'com.alibaba.fastjson.JSON'
2020-08-17 15:20:43,984 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSON; //获取j . . . '' : Typed variable declaration
2020-08-17 15:20:43,984 WARN o.a.j.a.BeanShellAssertion: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSON; //获取j . . . '' : Typed variable declaration
从报错日志看,是
toJSONString( com.alibaba.fastjson.JSONObject, com.alibaba.fastjson.serializer.SerializerFeature ) 这个方法在JSON类中找不到。
原因:fastjson中JSON类中toJSONString方法的features参数为可变长度参数列表
fastjson源码:
public static String toJSONString(Object object, SerializerFeature... features) {
return toJSONString(object, DEFAULT_GENERATE_FEATURE, features);
}
解决方案
修改脚本,传入定长数组
SerializerFeature[] features = new SerializerFeature[1];
features[0]=SerializerFeature.DisableCircularReferenceDetect;
vars.put("udx_flinksql_data",JSON.toJSONString(udx_flinksql_data, features));
运行成功