在JSR223Listener中创建JSON脚本时出现问题

时间:2017-11-08 09:34:55

标签: json jmeter jmeter-plugins

我在JSR233监听器中创建了一个JSON脚本。 我创建了这个JSon脚本,用于将一些数据发布到数据库中并收到以下错误消息:

2017-11-07 23:03:14,096 INFO o.a.j.v.JSR223Listener: ExceptionKPI :groovy.lang.MissingMethodException: No signature of method: org.apache.http.client.methods.HttpPost.setEntity() is applicable for argument types: (groovy.json.JsonBuilder) values:.

Please find below code
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import java.text.SimpleDateFormat;
import org.apache.jmeter.services.FileServer;
import groovy.json.*
def jsonBuilder = new groovy.json.JsonBuilder()
jsonBuilder {
     vars.get("testId")
     sampleResult.getSampleLabel()
    applicationName vars.get("applicationName")
    conversationId vars.get("ID")
    size sampleResult.getBytesAsLong()
    status Status
    messages FileServer.getFileServer().getScriptName()
    executionTimestamp System.currentTimeMillis()
    timeTaken sampleResult.getTime()
}
sampler.addNonEncodedArgument("",jsonBuilder.toPrettyString(),"")
sampler.setPostBodyRaw(true)

log.info("JASON OutPUT-----"+JsonOutput.prettyPrint(JsonOutput.toJson(jsonBuilder)))
            // Set Request URL
            HttpPost request = new HttpPost("${URL}");

    //      StringEntity params = new StringEntity(json.toString());
            request.addHeader("content-type", "`enter code here`application/json");
            //request.setEntity(jsonBuilder);
            request.setEntity(jsonBuilder.toPrettyString());
            HttpResponse response = httpClient.execute(request);
            log.info("HttpResponse response---------------------------------------------------------:" +response); 

2 个答案:

答案 0 :(得分:0)

您需要更改一行:

setEntity(jsonBuilder)

setEntity(jsonBuilder.toPrettyString())

参考文献:

答案 1 :(得分:0)

您缺少转换为实体对象StringEntity

 setEntity( new StringEntity(jsonBuilder.toPrettyString()))