- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我在 Eclipse 中的普通 java 项目中的 java 文件中执行以下代码时,它运行成功:
given()
.log().all()
.contentType("application/json")
.auth().basic(APIKEY, PASSWORD)
.body("{\"customName\":\"name\",\"customGender\":\"male\",\"customDateofBirth\":\"2010-12-08\",\"customRelationship\":\"child\"}")
.when()
.post("https://api.bamboohr.com/api/gateway.php/companyName/v1/employees/1420/tables/customDependants(IN)")
.then().assertThat().statusCode(200);
执行成功后会在指定表中添加一条新记录;(customDependants(IN),此处的表)。
然而,当我在 Spring Boot 项目中运行相同的内容时,出现错误:
java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <400>.
仅供引用:我尝试使用其他请求参数代替 contentType("application/json")
,例如:headers("Content-Type", ContentType.JSON)
。
错误日志:
HTTP/1.1 400 Bad Request
Server: nginx
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-BambooHR-Error-Messsage: Malformed XML
X-BambooHR-Error-Message: Malformed XML
Cache-Control: no-cache
Date: Tue, 31 Dec 2019 02:45:30 GMT
X-BambooHR-Error-Message: Malformed XML
X-BambooHR-Error-Messsage: Malformed XML
请求日志:
Request method: POST
Request URI: https://api.bamboohr.com/api/gateway.php/companyName/v1/employees/1420/tables/customDependants%28IN%29
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=*/* Content-Type=application/json ; charset=UTF-8
Cookies: <none>
Multiparts: <none>
Body: {
"customName": "name",
"customGender": "male",
"customDateofBirth": "2010-12-08",
"customRelationship": "child"
}
Spring Boot 代码中出现此错误:
private void employeeDependents(int empId) {
/*Employees Repository*/
TaBhr taBhr = taBhrRepository.findByBhrId(empId);
/*Grabbing an employee of given ID from Employees Repository*/
Optional<Employee> employee = employeeRepository.findById(taBhr.getEmpId());
Gson gson = new GsonBuilder().serializeNulls().create();
List<EmployeeDependent> list = employee.isPresent()
? employeeDependentRepository.findByDependentsEmpnumber(employee.get().getNumber())
: null;
if (list != null) {
for (EmployeeDependent employeeDependent : list) {
/*Sets the Data Transfer Object*/
EmployeeDependentDTO employeeDependentDTO = new EmployeeDependentDTO();
employeeDependentDTO.setFirstName(employeeDependent.getEdName());
employeeDependentDTO.setDateOfBirth(employeeDependent.getEdDateOfBirth());
employeeDependentDTO.setRelationship(employeeDependent.getDependentGender().getEdRelationshipType());
employeeDependentDTO.setGender(employeeDependent.getDependentGender().getGender());
TaBhr tabhr = taBhrRepository.findByEmpId(employee.get().getId());
if (tabhr != null) {
#LOG.info("BHR ID: {}", empId);#
given().log().all().contentType("application/json").auth()
.basic(APIKEY, PASSWORD)
.body(gson.toJson(employeeDependentDTO)).when()
.post("https://api.bamboohr.com/api/gateway.php/companyName/v1/employees/1420/tables/customDependants(IN)")
.then().log().ifError().assertThat().statusCode(200);
}
}
}
}
当我运行时,它会一直运行到被 # 包围的 LOG,然后当 API 被调用时,响应是 Malformed XML。 gson.toJson(employeeDependentDTO)
的值为 {"customName":"abcd","customGender":"male","customDateofBirth":"2010-12-08","customRelationship":"child"}
。
此外,这里我将内容类型设置为 JSON 而不是 XML,甚至负载也是 JSON 格式,适用于 postman 和非 spring boot 项目。
仅供引用:所有获取请求都在 Spring Boot 应用程序中完美运行,当我尝试使用帖子访问 API 时,只会收到上述错误,而如果我在非 Spring 应用程序中执行相同的操作,则一切正常。在 postman 中同样工作正常。
请帮助我解释为什么它无法在 Spring Boot 应用程序中执行。
最佳答案
我在这里使用了另一种解决方案,在 java 中使用 HttpURLConnection,它工作得很好,下面是替换上面放心代码的代码:
try {
String rawData = "{\"customName\":\"success\",\"customGender\":\"male\",\"customDateofBirth\":\"2010-12-08\",\"customRelationship\":\"child\"}";
String url = "https://api.bamboohr.com/api/gateway.php/companyName/v1/employees/1420/tables/customDependants(IN)";
URL obj = new URL(url);
HttpURLConnection myURLConnection = (HttpURLConnection) obj.openConnection();
String userCredentials = APIKEY:PASSWORD;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
myURLConnection.setRequestProperty("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/json");
myURLConnection.setRequestProperty("Accept", "application/json");
myURLConnection.setDoOutput(true);
byte[] outputInBytes = rawData.getBytes("UTF-8");
OutputStream w = myURLConnection.getOutputStream();
w.write(outputInBytes, 0, outputInBytes.length);
w.close();
int responseCode = myURLConnection.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Response code : " + responseCode);
System.out.println(response.toString());
// Use Jsoup on response to parse it if it makes your work easier.
} catch (Exception e) {
e.printStackTrace();
}
如果有人可以分享他们对前一种方法的解决方案,那将会很有用。
关于java - 我在处理 REST Assured API POST 请求时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59539119/
我有一个关于 Rest Assured 的问题,它是否只适用于 Java 测试?或者我可以将它与 C# 一起使用? 谢谢 最佳答案 编辑:顺便说一句,似乎也有一个 .net 实现: https://g
我需要针对以下 json 响应正文中“字段”(即“同意”、“失败计数”)下的值进行提取和/或断言: { "next": null, "previous": null, "results": [
我的示例 Web 服务返回以下 XML。 Wrong ID 2 以下测试通过。 response.then().body("errorResponse.errorId", Matchers.is
我正在尝试使用 REST Assured检查我的服务器返回的 HTML 文档的一些属性。安SSCCE问题的证明如下: import static com.jayway.restassured.path
io.rest-assured 和 jayway rest-assured 有什么区别?我应该使用哪个框架进行 restfull web 服务测试? 最佳答案 REST Assured 有一个新的组
我有一个 JSON 响应: ["alice", "jason", "steve", "alex"] 那么在使用的时候放心测试: when(). get("/names"). then()
我从未使用过JUnit或其他测试框架。我所知道的是如何发展休息服务。我最近看到了REST有保证的框架来测试REST API。但是我发现的所有文章如下所示。但是我不知道如何传递请求xml,如何获得响应以
我有一个 Spring Boot 应用程序,一个 Controller 将根据 post 参数重定向到一个页面。 我正在创建想要断言重定向页面的测试用例 但是我没能从放心的响应中得到重定向的html
我想放心地提取请求的URL,我尝试使用given().log().all()它正在记录所有内容,我只想提取我的请求 URI。 最佳答案 given().log().uri() 将在控制台中打印请求 u
我正在寻找一种在“放心”中实现条件验证的方法。假设我得到的响应正文为 { "id": 1, "accounts": [ { "accountType": "Type1", "pro
我正在尝试使用 REST 保证来测试我的登录/注销功能。是否有可能进行 REST 保证测试,发布登录然后发布注销?如果没有,我该如何正确测试? 最佳答案 就发两个后()与一个 断言()/期望() :
有没有办法让我拥有多个 Rest Assured Client 实例? 我必须测试需要的两个不同端点: 不同的序列化器和反序列化器。 不同的标题和内容类型。 我怎样才能做到这一点?我认为 Rest A
我有用于按名称查找用户的 REST,对于某些搜索词,它会返回在名字或姓氏中包含该词的用户。 GET /search/joe 返回 json 数组: [ {"id": 1, "firstName":
无法在我的端点 URL 中使用查询 我尝试过使用 .queryParams() 但它似乎不起作用。我收到以下错误 java.lang.IllegalArgumentException: Invalid
我有一个断言值的 API 测试。目前它从 Excel 读取行,然后在正常的 for 循环中循环。如果值确实匹配,则效果非常好。 但是,如果断言失败,则测试将以堆栈跟踪 AssertionError 结
我正在使用 Rest assured,我想知道如何更改运行它的数据库? 目前,我正在针对我的应用程序进行测试,该应用程序连接到保存我所有数据的数据库,但速度很慢。我要放心使用我的测试数据库。我怎样才能
在这种情况下有人可以帮助我吗: 当我调用这个服务时,http://restcountries.eu/rest/v1/,我得到了几个国家的信息。 但是,当我想获取任何特定国家/地区的信息(例如芬兰)时,
假设我有一个 Java Person类: class Person { String name; String email; } 使用 REST Assured,您可以反序列化这个 J
如何在放心的 API 测试中处理上传文件? 比如你必须使用 post(/url) 然后在特定参数上传文件。? 最佳答案 希望这会有所帮助,对我来说很好 given(). multiPa
在通过 RestAssured API 调用后,我从 REST API 获得了响应到 Response 对象。 响应主体是json,我想从中获取特定的键值? 代码如下 Response res = g
我是一名优秀的程序员,十分优秀!