4

I have a resource A that have a controller containing an endpoint for retrieving paged A items:

      public ResponseEntity getAllAs(
      @PageableDefault(size = 25, value = 0) Pageable pageable) {
        Page<A> pagedAs = .....
        return ResponseEntity.ok(pagedAs);
  }

When I have tried to create an integration test and calling this endpoint using TestRestTemplate, I got a problem because a Page object cannot be instantiated.

Here is the call:

    ResponseEntity<Page> response =  template.getForEntity("/api/as,
            Page.class );

And here is the exception:

  com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.data.domain.Page` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (PushbackInputStream); line: 1, column: 1]

I guess it is a normal regarding the fact that Page cannot be instantiated but this constrains the testability when using spring boot paginated results.

Master Mind
  • 3,014
  • 4
  • 32
  • 63
  • Possible duplicate of [How to consume Page response using Spring RestTemplate](https://stackoverflow.com/questions/34099559/how-to-consume-pageentity-response-using-spring-resttemplate) – jhyot Aug 07 '18 at 19:18
  • @jhyot in the chosen answer of that question the last comment says "I still gives me the error" – Master Mind Aug 07 '18 at 19:22
  • that's because the commenter still uses an abstract class inside his concrete one. The accepted example to my linked question should work. – jhyot Aug 07 '18 at 19:26

1 Answers1

0

Make changes in the response of the controller from Page<T> to List<T>.

public ResponseEntity getAllAs(
       @PageableDefault(size = 25, value = 0) Pageable pageable) {
       Page < A > pagedAs = .....
       return ResponseEntity.ok(pagedAs);
   }

Then convert where you are consuming the API from List to Page

Page<T> page = new PageImpl<>(list);
4b0
  • 21,981
  • 30
  • 95
  • 142
vedavyasa k
  • 1,173
  • 1
  • 7
  • 4