【问题标题】:Issue while creating a JSON script in JSR223Listener在 JSR223Listener 中创建 JSON 脚本时出现问题
【发布时间】:2017-11-08 09:34:54
【问题描述】:

我在 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); 

【问题讨论】:

  • 请添加相关代码
  • 按您的要求添加代码

标签: json jmeter jmeter-plugins


【解决方案1】:

你需要换行:

setEntity(jsonBuilder)

setEntity(jsonBuilder.toPrettyString())

参考资料:

【讨论】:

    【解决方案2】:

    您缺少将 StringEntity 转换为实体对象

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

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 2020-09-12
      • 2021-02-02
      • 2022-10-18
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多