我只想POST json请求(使用resassured),对于这样的json:
{
"userId": 1,
"id": 111,
"title":"test msg"
"body": "this is test msg"
}
我正在定义基本 uri,并尝试使用 hashmap:
RestAssured.baseURI = "http://localhost:8888";
RestAssured.basePath = "/posts/";
Map<String, String> map = new HashMap<String, String>();
map.put("userId", 1);
map.put("id", 111);
map.put("title","test Post");
map.put("body","this is test body");
当然它是“红色”,因为尝试将整数作为字符串。
我将 1 和 111 号码更改为 String.valueOf(),
然后成功发布请求,例如
given()
.contentType("application/json")
.body(map)
.when()
.post("/")
.then()
.statusCode(201);
但是响应不正确(与所需的json相比):
{
"id": "111",
"title": "test Post",
"body": "this is test body",
"userId": "1"
}
这里有2点:
- id and userId posted as Strings
- order is incorrect
那么,问题: 在这种情况下,以正确的顺序正确发布所需的请求并使用 id 和usedId 的int 值的最佳方法是什么?
谢谢!
最佳答案
你可以做的是使用Map<String, Object>
而不是Map<String,String>
。
此外,不会保留 JSON 对象的顺序。您不能也不应该依赖 JSON 对象中元素的顺序。
An object is an unordered set of name/value pairs. You can check out JSON specification for more info.
关于java - 放心 : posting json request having both String and Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59371173/