我正在解析json并验证数组长度,如下所示
from("direct:parseJson")
.setHeader("numberOfBooks").jsonpath("$..books.length()", int.class)
.choice()
.when( simple("${header.numberOfBooks} == '1'"))
.log("One book")
.otherwise()
.log("multiple");
以上代码有效,但我正在寻找可以避免第二行的选项 下面
.when( simple("${jsonpath(' $..books.length() ', int.class)} == 1") )
其抛出错误
Caused by:
org.apache.camel.language.simple.types.SimpleIllegalSyntaxException:
Unknown function: jsonpath(' $..books.length() ', int.class) == 1 at
location 0
${jsonpath(' $..books.length() ', int.class)} == 1
第二次尝试 我将标头设置为常数以验证简单表达式是否有效
.when().jsonpath("$..books.length() > ${header.numberOfBooks}")
Caused by: com.jayway.jsonpath.InvalidPathException: Could not parse token starting at position 18
at com.jayway.jsonpath.internal.path.PathCompiler.fail(PathCompiler.java:616) ~[json-path-2.4.0.jar:2.4.0]
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:152) ~[json-path-2.4.0.jar:2.4.0]
答案 0 :(得分:1)
您可以直接在when子句中使用jsonPath,如camel documentation中所述。您的代码如下所示:
from("direct:parseJson")
.choice()
.when().jsonpath("$..books.length() == '1'")
.log("One book")
.otherwise()
.log("multiple");