I need to call an endpoint which expects a Pageable
field:
@GetMapping
public Page<ProductDTO> listProducts(Pageable pageable) {
return productService.findProducts(pageable);
}
In my test I have this code:
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("page", String.valueOf(0));
URI url = defaultURI(port, "/products", parameters);
ParameterizedTypeReference<RestResponsePage<ProductDTO>> type = new ParameterizedTypeReference<RestResponsePage<ProductDTO>>() {};
ResponseEntity<RestResponsePage<ProductDTO>> response = restTemplate.exchange(url.toString(), HttpMethod.GET, httpEntity, type);
PageImpl
contains no default constructor so to avoid that problem I created a class like the following one to pass to the ParameterizedTypeReference
:
@JsonIgnoreProperties(ignoreUnknown = true) @Getter @Setter
public class RestResponsePage<T> extends PageImpl<T> implements Serializable {
private static final long serialVersionUID = 3844794233375694591L;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public RestResponsePage(@JsonProperty("content") List<T> content,
@JsonProperty("number") int page,
@JsonProperty("size") int size,
@JsonProperty("totalElements") long totalElements) {
super(content, new PageRequest(page, size), totalElements);
}
public RestResponsePage(List<T> content, Pageable pageable, long totalElements) {
super(content, pageable, totalElements);
}
public RestResponsePage(List<T> content) {
super(content);
}
public RestResponsePage() {
super(new ArrayList<T>());
}
}
The problem is that I still get the following error:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.data.domain.Pageable: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: java.io.PushbackInputStream@75564689; line: 38, column: 16] (through reference chain: com.shaunyl.util.ResponsePageImpl["pageable"])
Why it keeps saying that I am passing an abstract class? ResponsePageImpl
is a class not an abstract class.
Thank you
spring boot 2.0 may be ok above..