I am trying to catch and validate the response of an API but when I tried to debug the same, I am getting null values inside each of the variables of the developer DTO I am trying to reuse. Below is the code I am trying to use.
ValidatableResponse response = given().header("Authorization", token).header("Content-type", "application/json")
.when().log().all().pathParam("CalendarId", testCaseBean.getCalendarId().toString()).urlEncodingEnabled(false)
.queryParam("from", testCaseBean.getStartDate()).queryParam("to", testCaseBean.getEndDate())
.queryParam("monthEnd", testCaseBean.getMonthEndBusinessDay())
.get(EndPoint.GET_CALENDAR_DETAILS_BY_MULTIPLE_CALENDAR_CODE).then().log().all();
InCalendarDateResponseWrapper actualRIOutput = CommonUtils.getJSONMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.readValue(response.extract().asString(), InCalendarDateResponseWrapper .class);
String t=actualRIOutput.getCalendarId();
The value of t when I am trying to print, I am getting null. Below is the developer DTO.
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.time.LocalDate;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class InCalendarDateResponseWrapper {
private String calendarId;
private LocalDate calDat;
private LocalDate prevBus;
private LocalDate nextBus;
private Boolean bus;
private Boolean monthEnd;
}
The response of the GET API is as follows.
{
"EU": [
{
"calendarId": "EU",
"calDat": "2022-11-01",
"prevBus": "2022-10-31",
"nextBus": "2022-11-02",
"bus": true,
"monthEnd": false
}
],
"AU": [
{
"calendarId": "AU",
"calDat": "2022-11-01",
"prevBus": "2022-10-31",
"nextBus": "2022-11-02",
"bus": true,
"monthEnd": false
}
]
}
The getJSONMapper code which I am using is as follows.
public static ObjectMapper getJSONMapper() {
objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
}
Am I doing any mistake in catching the response? The response is returned by the API on the console when I do log().all() but when I try to fetch the response deserializing it, I see null values inside each of my variables one of them I have printed and it gives null on the console.
2
Answers
You need one more object.
To convert JSON to DTO
You dont need the line
You will need to annotate your
LocalDate
properties with annotation as follows:Please see this question and answer for detailed explanation: Spring Data JPA – ZonedDateTime format for json serialization