json 无法识别的字段“id”(类pojoClasses.BookingDetails),未标记为可忽略

8dtrkrch  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在运行一个RestApi,通过放心更新,获得错误无法解决Bookingdetails中的问题,没有ID。但在行中,我在行中获得错误

  • 无法识别的字段“id”(类pojoClasses.BookingDetails),未标记为可忽略(3个已知属性:“标题”、“作者”、“创建时间”])*

我有课
BookingDetails.java

package pojoClasses;

import com.fasterxml.jackson.annotation.JsonProperty;

public class BookingDetails {
    
    @JsonProperty
    private String title;
    
    @JsonProperty
    private String author;

    @JsonProperty
    private String createdat;

    public String gettitle(String title) {
        return this.title;
    }
    
    public void settitle(String title) {
        this.title = title;
    }
    
    public String getAuthor(String author) {
        return this.author;
    }
    
    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCreatedat(String createdat) {
        return this.createdat;
    }

    public void setcreatedat(String createdat) {
        this.createdat = createdat;
    }

}

更新预订.java

import io.restassured.response.Response;
import org.testng.annotations.Test;
import pojoClasses.BookingDetails;
import utility.AllureLogger;
import utility.BaseTest;

import static io.restassured.RestAssured.given;

public class UpdateBooking extends BaseTest {
    
    @Test(description="To update the details of the booking IDs") 
    public void updateBooking(){
        
        AllureLogger.logToAllure("Starting the test to update details");
        //Sending the PUT request for a specific booking id and receiving the response after updating the detals
        AllureLogger.logToAllure("PUT update booking detail");
        //Created a new booking
        CreateBooking createBooking = new CreateBooking();
        createBooking.createNewBooking("time", "psos", "2018-01-03", "null");
        String IDtoUpdate = createBooking.newID;
        AllureLogger.logToAllure("New Booking ID created is : "+IDtoUpdate);
        System.out.println("IDtoUpdate:::::::   "+IDtoUpdate);
        //Update the booking with new first name
        Response getResponse = given().
                spec(requestSpec).
                pathParam("id", IDtoUpdate).
            when().
                get("/posts/{id}");

        BookingDetails bookingDetails = getResponse.as(BookingDetails.class);
        bookingDetails.settitle("employee");
        Response response = given().
            spec(requestSpec).
            header("Content-Type", "application/json").
            header("Accept", "application/json").
//          header("Cookie", cookieValue).
            pathParam("id", IDtoUpdate).
//          pathParam("id", 3).
            body(bookingDetails).log().body().
        when().
            put("/posts/{id}");
        
        //Verify the response code
        AllureLogger.logToAllure("Asserting the response if the status code returned is 200");
        response.then().spec(responseSpec);     

        //To log the response to report
        logResponseAsString(response);
        
    }
}

通过命令-〉mvn install运行时

输出

Body:
{
    "title": "time",
    "author": "psos",
    "createdat": "2018-01-03"
}
{
  "title": "time",
  "author": "psos",
  "createdat": "2018-01-03",
  "id": 32
}
32
IDtoUpdate:::::::   32
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 21.409 s <<< FAILURE! - in TestSuite
[ERROR] updateBooking(UpdateBooking)  Time elapsed: 10.263 s  <<< FAILURE!
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "id" (class pojoClasses.BookingDetails), not marked as ignorable (3 known properties: "title", "author", "createdat"])
 at [Source: (String)"{
  "title": "time",
  "author": "psos",
  "createdat": "2018-01-03",
  "id": 32
}"; line: 5, column: 11] (through reference chain: pojoClasses.BookingDetails["id"])
        at UpdateBooking.updateBooking(UpdateBooking.java:30)
nbysray5

nbysray51#

我在BookingDetails DTO/POJO中没有看到ID字段。请尝试如下添加:

private String id;

 public String getId() {
        return this.id;
    }

相关问题