我有以下REST控制器:
@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
}
}
以下存储库:
interface PersonRepository extends PagingAndSortingRepository<Person,Integer> {
}
服务:
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)
}
}
服务器按预期工作,但我在客户端遇到问题。我不知道如何从响应中获取内容。
在客户端我有这样的方法:
@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){}
}
但我得到以下例外:
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)
现在我读了这个stackoverflow-entry并添加了CustomPageImpl类:std::remove_if
我将客户端中的方法更改为以下内容:
@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){}
}
但现在我得到了几乎相同的例外:
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
答案 0 :(得分:0)
在您提到的相同堆栈溢出条目中,有一个交换请求的解决方案。您可以使用PagedResources
代替Page / Custom实现。 Refer this link了解更多详情。
以下是您的要求: -
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