Home > Net >  How to create POJO class which store list of key value pair in java for a request body?
How to create POJO class which store list of key value pair in java for a request body?

Time:09-21

{
    "availabilityMap": {
        "2021-07-20":["10PM-11PM" , "11PM-11:30PM"],
        "2021-07-20":["9PM-10PM" , "10PM-11:00PM"]
    }
}

The above is the Request Body. I have created below Dto class

@Getter
@Setter
public class AvailabilityDto {
    private AvailabilityMap availabilityMap;

    @Override
    public String toString() {
        return "AvailabilityDto{"  
                "availabilityMap="   availabilityMap  
                '}';
    }
}
@Getter
@Setter
public class AvailabilityMap {
    HashMap<LocalDate,List<String>> availableDates;

    @Override
    public String toString() {
        return "AvailabilityMap{"  
                "availableDates="   availableDates  
                '}';
    }
}

I am new to springboot and restApi. availableDates showing null

CodePudding user response:

I suspect that you may need to register JavaTimeModule in your Jackson ObjectMapper as follows so that LocalDate can be properly deserialized:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

You also need the following dependency:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

CodePudding user response:

I created AvailabilityDto as below and it worked.

public class AvailabilityDto {
    private LinkedHashMap<LocalDate, List<String>> availabilityMap;

    @Override
    public String toString() {
        return "AvailabilityDto{"  
                "availabilityMap="   availabilityMap  
                '}';
    }
}
  • Related