gpt4 book ai didi

java - 使用 Rest Assured 发出 POST 请求时出现内部服务器错误

转载 作者:行者123 更新时间:2023-12-01 16:43:53 25 4
gpt4 key购买 nike

我尝试使用 POJO 发出 POST 请求,但收到“vHTTP/1.1 500 内部服务器错误”,您能否告知原因?当我将正文作为字符串传递时,它工作正常,但传递 bookingDetails 会引发错误。我认为 BookingDates 类有些问题,但我不确定为什么。

package bookings.POST;

import bookings.BookingBaseTest;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;


public class CreateBooking extends BookingBaseTest {
@Test
public void createBooking(){
//Set json Data
BookingDetails bookingDetails = new BookingDetails();
bookingDetails.setFirstname("Jim");
bookingDetails.setLastname("Brown");
bookingDetails.setTotalprice(111);
bookingDetails.setDepositpaid(true);

BookingDetails.BookingDates bDates = new BookingDetails.BookingDates();
bDates.setCheckin("2018-01-01");
bDates.setCheckout("2019-01-01");

bookingDetails.setBookingDates(bDates);
bookingDetails.setAdditionalneeds("Breakfast");


Response response = RestAssured.given().
spec(requestSpec)
.body(bookingDetails)
.when()
.post();
response.then().log().all();


}


}
package bookings.POST;
/*
{
"firstname" : "Jim",
"lastname" : "Brown",
"totalprice" : 111,
"depositpaid" : true,
"bookingdates" : {
"checkin" : "2018-01-01",
"checkout" : "2019-01-01"
},
"additionalneeds" : "Breakfast"
}
*/

public class BookingDetails {
private String firstname;
private String lastname;
private int totalprice;
private boolean depositpaid;
private BookingDates bookingDates;
private String additionalneeds;

//Omit other getters and setters


public BookingDates getBookingDates() {
return bookingDates;
}

public void setBookingDates(BookingDates bookingDates) {
this.bookingDates = bookingDates;
}


static class BookingDates {
private String checkin;
private String checkout;

public String getCheckin() {
return checkin;
}

public void setCheckin(String checkin) {
this.checkin = checkin;
}

public String getCheckout() {
return checkout;
}

public void setCheckout(String checkout) {
this.checkout = checkout;
}

}
}

最佳答案

您应该在请求正文中传递 BookingDetails 对象的 json 序列化正文。您可以使用 ObjectMapper 来序列化对象:

//serialize the bookingDetails object 
ObjectMapper mapper = new ObjectMapper();
String bookingDetailsJson = mapper.writeValueAsString(bookingDetails);

然后将其传递到请求正文中,同时将 contentType 指定为 JSON:

Response response = RestAssured.given().spec(requestSpec).contentType(JSON).body(bookingDetailsJson).when().post();

关于java - 使用 Rest Assured 发出 POST 请求时出现内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61813372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com