skip to Main Content

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


  1. You need one more object.

    import com.fasterxml.jackson.annotation.JsonProperty;
    
    @Data
    public class Wrapper {
        @JsonProperty("EU")
        private List<InCalendarDateResponseWrapper> EU;
        
        @JsonProperty("AU")
        private List<InCalendarDateResponseWrapper> AU;
    }
    

    To convert JSON to DTO

    import io.restassured.path.json.JsonPath;
    ...
    Wrapper wrapper = JsonPath.from(res).getObject("", Wrapper.class);
    String t = actualRIOutput.getEU().get(0).getCalendarId();
    
    Login or Signup to reply.
  2. You dont need the line

    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    

    You will need to annotate your LocalDate properties with annotation as follows:

        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
        private LocalDate calDat;
    

    Please see this question and answer for detailed explanation: Spring Data JPA – ZonedDateTime format for json serialization

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search