RestAssured:发布 json 具有字符串和整数的请求

RestAssured: posting json request having both String and Integer

我只是想POSTjson请求(使用放心),对于这样的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");

当然还有 "red",因为尝试将整数作为字符串。

我正在为我的 1 和 111 号码更改​​为 String.valueOf(),

然后成功 post 请求

 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

所以,问题: 在这种情况下,正确 post 需要的请求的最佳方法是什么,以正确的顺序并使用 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.