【问题标题】:Rest-assured. Is it possible to extract value from request json?放心。是否可以从请求 json 中提取值?
【发布时间】:2014-02-05 14:48:27
【问题描述】:

我收到这样的回复:

Response response = expect().statusCode(200).given().body(requestBody).contentType("application/json")
.when().post("/admin");
String responseBody = response.getBody().asString();

我在 responseBody 中有一个 json:

{"user_id":39}

我可以只使用放心的方法提取到字符串这个值 = 39 吗?

【问题讨论】:

  • 尝试查找有关如何在 Java 中解析 JSON 的信息——将 JSON(在您的情况下)转换为 Map。不幸的是,您会发现大约 20 种不同的方法来实现它,其中大多数都过于复杂,但 Java 专家似乎喜欢这种方式。
  • 谢谢,@HotLicks,我知道这个决定,我只是在寻找放心的答案。好像做不到。
  • @Jay 这是一个老问题,但在我看来它被贴错标签了。您的标题是“从请求 json 中提取值”,不应该是“从响应 json 中提取标签”吗?以下所有答案都假设您的意思是响应...

标签: java json rest rest-assured


【解决方案1】:

你也可以直接使用响应对象。

Response response = expect().statusCode(200).given().body(requestBody).contentType("application/json").when().post("/admin");

String userId = response.path("user_id").toString();

【讨论】:

    【解决方案2】:

    要将响应序列化为一个类,请定义目标类

    public class Result {
        public Long user_id;
    }
    

    并映射响应:

    Response response = given().body(requestBody).when().post("/admin");
    Result result = response.as(Result.class);
    

    您必须在 documentation 中所述的类路径中包含 Jackson 或 Gson。

    【讨论】:

    • 这种经典的 DTO 方法适用于小的静态响应。但是维护所有样板文件的开销是临时/动态/大型响应的拖累。
    【解决方案3】:
    JsonPath jsonPathEvaluator = response.jsonPath();
    return jsonPathEvaluator.get("user_id").toString();
    

    【讨论】:

      【解决方案4】:

      有几种方法。我个人使用以下几种:

      提取单个值:

      String user_Id =
      given().
      when().
      then().
      extract().
              path("user_id");
      

      当您需要多个响应时,使用整个响应:

      Response response =
      given().
      when().
      then().
      extract().
              response();
      
      String userId = response.path("user_id");
      

      使用 JsonPath 提取一个以获得正确的类型:

      long userId =
      given().
      when().
      then().
      extract().
              jsonPath().getLong("user_id");
      

      当您想要匹配值和类型时,最后一个非常有用,即

      assertThat(
          when().
          then().
          extract().
                  jsonPath().getLong("user_id"), equalTo(USER_ID)
      );
      

      放心的文档描述性很强且完整。有很多方法可以实现您的要求:https://github.com/jayway/rest-assured/wiki/Usage

      【讨论】:

        【解决方案5】:

        如果您只对提取“user_id”感兴趣,也可以这样做:

        String userId = 
        given().
                contentType("application/json").
                body(requestBody).
        when().
                post("/admin").
        then().
                statusCode(200).
        extract().
                path("user_id");
        

        最简单的形式如下:

        String userId = get("/person").path("person.userId");
        

        【讨论】:

          【解决方案6】:

          我找到了答案:)

          使用 JsonPathXmlPath(如果您有 XML)从响应正文中获取数据。

          就我而言:

          JsonPath jsonPath = new JsonPath(responseBody);
          int user_id = jsonPath.getInt("user_id");
          

          【讨论】:

          猜你喜欢
          • 2023-04-10
          • 2018-01-02
          • 2021-11-03
          • 1970-01-01
          • 2012-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-31
          相关资源
          最近更新 更多