Our Consumed REST APIs are returning results in Pages. Here is an example:
Event Bean class:
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties
public class Event implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7613296461448310227L;
private String name;
private String id;
private String type;
}
EventList Bean class:
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties
public class EventList {
@JsonProperty("events")
private List<Event> events = null;
}
Response Bean class:
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties
public class EventResponse {
@JsonProperty("_embedded")
private EventList responseList = null;
}
Logic to call Rest API with RestTemplate:
ParameterizedTypeReference<RestResponsePage<Event>> responseType = new ParameterizedTypeReference<RestResponsePage<Event>>() {
};
ResponseEntity<RestResponsePage<Event>> response = this.restTlsTemplate
.exchange(this.url, HttpMethod.GET, entity, responseType);
RestResponsePage<Event> eventsResources = response.getBody();
Collection<Event> eventsCollection = eventsResources.getContent();
RestResponsePage class:
@JsonIgnoreProperties(ignoreUnknown = true)
public class RestResponsePage<T> extends PageImpl<T> {
/**
*
*/
private static final long serialVersionUID = -5317908588846766606L;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public RestResponsePage(@JsonProperty("content") List<T> content,
@JsonProperty("number") int number,
@JsonProperty("size") int size,
@JsonProperty("totalElements") Long totalElements,
@JsonProperty("pageable") JsonNode pageable,
@JsonProperty("last") boolean last,
@JsonProperty("totalPages") int totalPages,
@JsonProperty("sort") JsonNode sort,
@JsonProperty("first") boolean first,
@JsonProperty("numberOfElements") int numberOfElements) {
super(content, PageRequest.of(number, size), totalElements);
}
public RestResponsePage(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
public RestResponsePage(List<T> content) {
super(content);
}
public RestResponsePage() {
super(new ArrayList<>());
}
}
It throwing an exception when trying with above:
Error = JSON conversion problem: Cannot construct instance of
....RestResponsePage, problem: Page size must not be less than one!; nested exception is com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of
....RestResponsePage, problem: Page size must not be less than one! at [Source: (PushbackInputStream); line: 81, column: 1] | JSON conversion problem: Cannot construct instance of
....RestResponsePage, problem: Page size must not be less than one!; nested exception is com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of
....RestResponsePage, problem: Page size must not be less than one! at [Source: (PushbackInputStream); line: 81, column: 1]
I have seen some posts similar to this issue but I has also trying the same and it didn't working for me. So can anyone please help me on this as i am not getting the issue and also I am new to this Page Deserialization with Rest Client.