0

I have the following REST-Controller:

@RestController
class PersonController {

   final PersonService personService

   @Autowired
   PersonController( PersonService personService ){
    this.personService = personService
   }

   @RequestMapping(value="/persons",method=RequestMethod.GET)
   Page<Person> list( Pageable pageable){
     Page<Person> persons = personService.listAllByPage(pageable)
     persons
   } 
}

The following repository:

interface PersonRepository extends PagingAndSortingRepository<Person,Integer> {

}

And the service:

interface PersonService {
   Page<Person> listAllByPage(Pageable pageable)
}

@Service
class PersonServiceImpl implements PersonService {

   final PersonRepository personRepository

   @Autowired
   PersonServiceImpl(PersonRepository personRepository){
      this.personRepository = personRepository
   }

   @Override
   Page<Person> listAllByPage(Pageable pageable) {
       personRepository.findAll(pageable)
   } 
}

The server works as expected, but I have a problem in the client. I don't know how to get the content from the response.

In the client I have a method like this:

@Override
public List<PersonDTO> findAll() throws DataAccessException {
    try {
        restClient.getServiceURI(PERSON_URL));
        ResponseEntity<PageImpl<PersonDTO>> response =
            restClient.exchange(
                restClient.getServiceURI(PERSON_URL),
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<PageImpl<PersonDTO>>() {
                });
        return response.getBody().getContent();
    } catch (Exception e){}
}

But I get the following exception:

 org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.data.domain.PageImpl]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.data.domain.PageImpl` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

Now I read this stackoverflow-entry and add the CustomPageImpl class: How to consume Page<Entity> response using Spring RestTemplate

I change the method in the client to the following:

@Override
public List<PersonDTO> findAll() throws DataAccessException {
    try {
        restClient.getServiceURI(PERSON_URL));
        ResponseEntity<CustomPageImpl<PersonDTO>> response =
            restClient.exchange(
                restClient.getServiceURI(PERSON_URL),
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<CustomPageImpl<PersonDTO>>() {
                });
        return response.getBody().getContent();
    } catch (Exception e){}
}

But now I get almost the same exception:

 org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.data.domain.Pageable]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.data.domain.Pageable` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
Steve2Fish
  • 187
  • 4
  • 15

1 Answers1

0

In the same stack overflow entry you mentioned, there is a solution for exchange request. You can use PagedResources instead of Page/Custom implementation. Refer this link for more details.

Here will be your request :-

ResponseEntity<PagedResources<PersonDTO>> response =
            restClient.exchange(
                restClient.getServiceURI(PERSON_URL),
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<PagedResources<PersonDTO>>() {
                });
PagedResources<PersonDTO> resBody = response.getBody();
        return resBody.getContent();// Returns a collection 
user8826113
  • 119
  • 5