使用Jayways Jsonpath保证休息?

时间:2018-05-29 14:53:24

标签: json rest-assured

我花了很长时间使用Jayways版本找出一些JsonPath,我的查询如下:

$.items[?(@.To[0].Mailbox == 'foobar')]..Body

但是,这不适用于Rest-Assured,执行时,我显示以下错误:

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found '[' @ line 1, column 34.
                            $.items[?(@.To[0].Mailbox == 'foobar')]..Body

我是否需要以GPath格式重写此表达式?如果是这样,那会是什么样子?或者我可以以某种方式让Rest-Assured使用这个首选的JsonPath表达式吗?

现在是一段小的 [WIP] 代码,只是试图让表达式正常工作,然后我会正确修复方法:

public String readInbox(String emailAddress) {
    Response response =  given().log().all().when().get(MESSAGES_ENDPOINT);
    JsonPath jsonPath = new JsonPath(response.asString());
    String emailAddress = jsonPath.getString("$.items[?(@.To[0].Mailbox == 'foobar')]..Body");
    System.out.println(accountVerificationEmailHyperlinkByEmailAddress);
    return "";
  }

1 个答案:

答案 0 :(得分:0)

通过完全排除放心并在响应体上使用jsonPath来解决这个问题:

emailBodies = JsonPath.read(given().when().get(MESSAGES_ENDPOINT).getBody().asString(), generatePayLoadForEmailPrefixOf(emailPrefix));