Java 使用 RestAssured 的 JSON POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34882203/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:05:06  来源:igfitidea点击:

JSON POST using RestAssured

javarest-assured

提问by user3520080

I am trying to create a test to validate the response of a JSON Post is as expected.

我正在尝试创建一个测试来验证 JSON Post 的响应是否符合预期。

I am trying to test the POST of a JSON message body to a URL which is turn then sends a text message and if successful it sends a response that it was successful again in JSON format.

我正在尝试将 JSON 消息正文的 POST 测试到一个 URL,然后该 URL 将发送一条文本消息,如果成功,它会以 JSON 格式再次发送成功的响应。

My test is as follows

我的测试如下

public void simpleTest() {

      String myJson = "{\"phoneNumber\":\"353837986524\", \"messageContent\":\"test\"}";
      given()
              .port(31111) // port number
              .header("Content-Type", "application/json")
              .body(myJson)
              .when()
              .post("/testenvironment/text/send")
              .then().assertThat()
              .body("message", equalTo("{\"resultMessage\":\"Message accepted\"}"));
  }

But seem to be getting this exception

但似乎得到了这个例外

java.lang.IllegalStateException: You can either send form parameters OR body content in POST, not both!

java.lang.IllegalStateException:您可以在 POST 中发送表单参数或正文内容,不能同时发送!

And I'm not sure what the issue is?

而且我不确定是什么问题?

回答by Bathiya Priyadarshana

Try changing the mimeType to a header instead of a parameter.

尝试将 mimeType 更改为标题而不是参数。

And based on the information you shared I think what you need is the Content-Type header, not mimeType.

根据您共享的信息,我认为您需要的是 Content-Type 标头,而不是 mimeType。

回答by parishodak

Restassured is failing to parse Json as per the stack trace. I use org.jsonjar, which is more elegant way to handle big json inputs. There are other implementations of json handling in java, which can be used based on your preference.

Restassured 无法根据堆栈跟踪解析 Json。我使用org.jsonjar,这是处理大 json 输入的更优雅的方式。java中还有其他json处理的实现,可以根据自己的喜好使用。

Coming to your code:

来到你的代码:

public void simpleTest() {

   // use org.json JSONObject to define your json
   JSONObject jsonObj = new JSONObject()
                             .put("phoneNumber","353837986524")
                             .put("messageContent","test");

   given()
      .port(31111) // port number
      .contentType("application/json")  //another way to specify content type
      .body(jsonObj.toString())   // use jsonObj toString method
   .when()
      .post("/testenvironment/text/send")
   .then()
      .assertThat()
      .body("message", equalTo("{\"resultMessage\":\"Message accepted\"}"));
}

Also, I didnt find what the output of the rest service in the question. For example it is returning a a json {"resultMessage":"Message accepted"}you should be validating the response in the following way:

另外,我没有在问题中找到其余服务的输出。例如,它返回 aa json,{"resultMessage":"Message accepted"}您应该通过以下方式验证响应:

...
.body("resultMessage",equalTo("Message accepted"));