Java 如何在SpringREST客户端中获取页面内容
我有以下REST控制器:Java 如何在SpringREST客户端中获取页面内容,java,spring,rest,pagination,client,Java,Spring,Rest,Pagination,Client,我有以下REST控制器: @RestController class PersonController { final PersonService personService @Autowired PersonController( PersonService personService ){ this.personService = personService } @RequestMapping(value="/persons",method=Req
@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
}
}
现在我阅读了这个stackoverflow条目并添加了CustomPageImpl类:
我将客户端中的方法更改为以下内容:
@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){}
}
在您提到的同一个堆栈溢出条目中,有一个针对exchange请求的解决方案。您可以使用
PagedResources
而不是页面/自定义实现。更多细节
以下是您的请求:-
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
ResponseEntity响应=
restClient.exchange(
restClient.getServiceURI(PERSON\uURL),
HttpMethod.GET,
无效的
新的ParameteredTypeReference(){
});
PagedResources resBody=response.getBody();
返回resBody.getContent();//返回一个集合
@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)
@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
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