简体   繁体   English

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

[英]Internal Server Error when making POST request using Rest Assured

I tried to make a POST request using POJO but getting "vHTTP/1.1 500 Internal Server Error", could you please advise why?我尝试使用 POJO 发出 POST 请求,但收到“vHTTP/1.1 500 内部服务器错误”,您能否告知原因? When I pass body as string it works fine, but the passing the bookingDetails will throw the error.当我将 body 作为字符串传递时,它工作正常,但传递 bookingDetails 会引发错误。 I think something is not right with the BookingDates class but I'm not sure why.我认为 BookingDates class 有问题,但我不知道为什么。

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;
        }

    }
}

You should pass the json serialized body of the BookingDetails object in your request body.您应该在请求正文中传递 BookingDetails object 的 json 序列化正文。 You can use ObjectMapper for serializing the object:您可以使用 ObjectMapper 序列化 object:

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

And then pass it in your request body, also mentioning the contentType as JSON:然后在您的请求正文中传递它,同时将 contentType 提到为 JSON:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM