Java 放心。是否可以从请求 json 中提取值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21166137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rest-assured. Is it possible to extract value from request json?
提问by Jay
I'm getting response this way:
我得到这样的回应:
Response response = expect().statusCode(200).given().body(requestBody).contentType("application/json")
.when().post("/admin");
String responseBody = response.getBody().asString();
I have a json in responseBody:
我在 responseBody 中有一个 json:
{"user_id":39}
Could I extract to string using rest-assured's method only this value = 39?
我可以使用放心的方法提取到字符串只有这个值 = 39 吗?
采纳答案by Jay
回答by Johan
You can also do like this if you're only interested in extracting the "user_id":
如果您只对提取“user_id”感兴趣,您也可以这样做:
String userId =
given().
contentType("application/json").
body(requestBody).
when().
post("/admin").
then().
statusCode(200).
extract().
path("user_id");
In its simplest form it looks like this:
最简单的形式如下:
String userId = get("/person").path("person.userId");
回答by Milanka
To serialize the response into a class, define the target class
要将响应序列化为类,请定义目标类
public class Result {
public Long user_id;
}
And map response to it:
并映射对它的响应:
Response response = given().body(requestBody).when().post("/admin");
Result result = response.as(Result.class);
You must have Hymanson or Gson in the classpath as the documentation states: http://rest-assured.googlecode.com/svn/tags/2.3.1/apidocs/com/jayway/restassured/response/ResponseBodyExtractionOptions.html#as(java.lang.Class)
如文档所述,您必须在类路径中包含 Hymanson 或 Gson:http: //rest-assured.googlecode.com/svn/tags/2.3.1/apidocs/com/jayway/restassured/response/ResponseBodyExtractionOptions.html#as( java.lang.Class)
回答by magiccrafter
There are several ways. I personally use the following ones:
有几种方法。我个人使用以下几个:
extracting single value:
提取单个值:
String user_Id =
given().
when().
then().
extract().
path("user_id");
work with the entire response when you need more than one:
当您需要多个响应时,使用整个响应:
Response response =
given().
when().
then().
extract().
response();
String userId = response.path("user_id");
extract one using the JsonPath to get the right type:
使用 JsonPath 提取一个以获得正确的类型:
long userId =
given().
when().
then().
extract().
jsonPath().getLong("user_id");
Last one is really useful when you want to match against the value and the type i.e.
当您想匹配值和类型时,最后一个非常有用,即
assertThat(
when().
then().
extract().
jsonPath().getLong("user_id"), equalTo(USER_ID)
);
The rest-assured documentation is quite descriptive and full. There are many ways to achieve what you are asking: https://github.com/jayway/rest-assured/wiki/Usage
放心的文档非常具有描述性和完整。有很多方法可以实现您的要求:https: //github.com/jayway/rest-assured/wiki/Usage
回答by PTT
JsonPath jsonPathEvaluator = response.jsonPath();
return jsonPathEvaluator.get("user_id").toString();