I am getting UnrecognizedPropertyException while executing a test script. I am using intellij-idea, org.projectlombok:lombok:1.18.26 , com.fasterxml.jackson.core:jackson-databind:2.14.2 and io.rest-assured:rest-assured:5.3.0 library with java 17.
In pojo class, if I make the class fields public then it works. But if I change the access specifiers as private then I am getting the below error.
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "page" (class com.SampleGradle.pojo.responsePojo.User), not marked as ignorable (5 known properties: "per_page", "data", "total", "support", "total_pages"]) .
However, same code is working with Eclipse with same configs.
User class
@Data
public class User {
private int page;
private int per_page;
private int total;
private int total_pages;
private List<Datum> data;
private Support support;
}
gradle build file
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'
testImplementation 'junit:junit:4.13.1'
implementation 'io.rest-assured:rest-assured:5.3.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
testImplementation 'org.assertj:assertj-core:3.24.2'
compileOnly 'org.projectlombok:lombok:1.18.26'
}
requestutil file
public Response getUsersList(String endPoint)
{
return RestAssured.given()
.contentType("application/json")
.get(endPoint)
.andReturn();
}
The UnrecognizedPropertyException occurs when you are trying to deserialize a JSON response into a Java object, but the JSON contains properties that do not exist in the corresponding Java class.
In your case, the exception is likely caused by the JSON response containing additional properties that are not present in the User class. To resolve this issue, you can use the @JsonIgnoreProperties(ignoreUnknown = true) annotation on the User class. This annotation tells Jackson to ignore any unrecognized properties in the JSON.
Here's an updated version of your User class with the @JsonIgnoreProperties annotation:
By adding @JsonIgnoreProperties(ignoreUnknown = true), Jackson will ignore any unknown properties during deserialization and prevent the UnrecognizedPropertyException from being thrown.
Make sure to import com.fasterxml.jackson.annotation.JsonIgnoreProperties in your code.
Additionally, ensure that the JSON response you are trying to deserialize matches the structure defined in the User class. If there are additional properties in the JSON that you want to capture, you can add them as fields in the User class.
I hope this resolves the issue for you. Let me know if you have any further questions!