在 class'com.jayway.jsonpath.Configuration 中找不到 Jmeter JSR223 方法 addOptions( com.jayway.jsonpath.Option )

Jmeter JSR223 Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration

我在 运行 Jmeter JSR223 采样器中的波纹管 java 代码时遇到错误

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import net.minidev.json.JSONArray;


String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";

        String jsonPath = "$..book[?(@.author == 'Nigel Rees')].title";

        Configuration config = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);

        JSONArray authorsArr = JsonPath.using(config).parse(json).read(jsonPath);

        System.out.println(authorsArr.get(0).toString());

json-path-2.4.0.jar 添加在 ..\lib\ext 下并假设它在 Jmeter 启动时自动加载。

上面的代码在 IDE 中检查过并且 运行 没问题。

JSR223 错误

2021-02-02 17:19:10,580 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL ) 
 in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL ) 
 in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
    at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_241]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:69) [ApacheJMeter_java.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
  1. 将“语言”更改为groovy,如果您选择java,则实际上不是Java,而是Beanshell[=19] =]

    • 不完全Java合规
    • 与 Groovy 相比,
    • 的性能要差得多

    所以according to JMeter Best Practices you should switch to Groovy since JMeter 3.1. More information: Apache Groovy - Why and How You Should Use It

  2. 更改此行:

    String jsonPath = "$..book[?(@.author == 'Nigel Rees')].title";
    

    到这个

    String jsonPath = '$..book[?(@.author == \'Nigel Rees\')].title'
    

    因为您的 Java 语法与 Groovy GString Template Engine

    略有冲突
  3. 您不应该需要任何额外的 .jars,该代码将适用于 vanilla JMeter 实例

还要注意 Groovy 有 built-in JSON support