java - 放心 : posting json request having both String and Integer

标签 java json rest-assured

我只想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/

相关文章:

javascript - 使用 Javascript 读取 JSON 文件

java - 如何使用 JSON 响应通过 Rest Assured 基于正文项进行断言?

java - 为什么我在使用 useDelimiter 时在我的数组中得到一个空元素

php - CakePHP - 只是布局?

java - 将 Accounts.UUID AND UUID = 与两个表一起使用时的 MySQL 语法问题

ios - 如何通过快速点击 Collection View 的单元格来重新加载 TableView 中的数据

java - 放心 : How can I get the RequestSpecification fields or parameters after sending a request?

java - 使用 Reassured 上传文件

java - Java中哪种方法适合这种场景?

java - 如何用应用程序上下文配置文件替换 web.xml?