JMeter BeanShell调用不定参数的方法

拿FastJSON下的类为例,
JSON类里有下面这个方法,用来做JSON字符串输出

public abstract class JSON implements JSONStreamAware, JSONAware {

......
    public static String toJSONString(Object object, SerializerFeature... features) {
        // dosomething
    }
......
}

通过可以这样直接调用:

String resultJSON = JSON.toJSONString(jsonObject,SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat)

这时问题来了,现在需要在JMeter里格式化输出JSON,也这样写入BeanShell

prev.setResponseData(JSON.toJSONString(jsonObject,SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat));

执行后就出错:Not found class "com.alibaba.fastjson.JSON"。

如果不看这个错误以为是没导入JSON类,但是事实上我们已经加入了import com.alibaba.fastjson.JSON这一行,不然前面几步用到JSON解析就无法执行。

 

解决方法:

将toJSONString需要的不定参数换成数组后传入就完成了,修改后的BeanShell代码如下:

SerializerFeature[] features = new SerializerFeature[3];
features[0]=SerializerFeature.PrettyFormat;
features[1]=SerializerFeature.WriteMapNullValue;
features[2]=SerializerFeature.WriteDateUseDateFormat;

prev.setResponseData(JSON.toJSONString(jsonObject, features));

修改后再执行就通过了,还可以看到输出结果里整齐的JSON字符串。

你可能感兴趣的:(Java)