PagedResources<Resource 和 RestTemplate 不起作用 [英] PagedResources<Resource and RestTemplate does not works
问题描述
我正在使用 Spring Boot REST 示例.在此,我使用 RestTemplate 调用返回 PagedResources
对象的端点.但是通过RestTemplate
调用时,却没有得到任何内容.然而,该服务构建在另一个微服务中,该服务可通过 Web 轻松访问并可通过 Postman 调用.
I am using Spring Boot REST example. In this I am using RestTemplate to call the endpoint which returns PagedResources<Resource<EmployeeDto>>
Object. But when calling through RestTemplate
, I did not get any contents. However this service is build in another microservice which easily accessible over web and can be call through Postman.
@GetMapping("/{employeeId}/employees")
public PagedResources<Resource<EmployeeDto>> getEmployyes(@PathVariable(name="employeeId") String employeeId,
@RequestParam(defaultValue="0",required = false, name="page") Integer page,
@RequestParam(defaultValue="25",required = false, name = "size") Integer size,
@RequestParam(defaultValue="billingNumber") String sortParam,
@RequestParam(defaultValue="ASC",required = false) Direction direction,
Pageable pageable, HttpServletRequest request) throws IOException{
return employeeService.getEmployeesByCid(employeeId, request);
}
我使用了下面的代码,但它没有给我任何内容.
I used below code and it gives me no contents.
String uri = "http://localhost:8080/employee-api/employees/160166/employees?page=0&size=25";
RestTemplate template = new RestTemplate();
ResponseEntity<PagedResources<Resource<EmployeeDto>>> studentResponse = template
.exchange(uri, HttpMethod.GET, null, new TypeReferences.PagedResourcesType<Resource<EmployeeDto>>(){});
System.out.println(studentResponse.getBody());
如果我在下面使用,那么我会得到响应.
If I used below, then I get the response.
final ResponseEntity<String> studentResponse = template
.exchange(URL, HttpMethod.GET, null, String.class);
注意:如果我通过 Postman 执行代码,我会得到以下响应.
Note: If I execute code through Postman I get below response.
{
"_embedded": {
"employeeDto": [
{
"employeeNumber": "3109194",
"status": "A"
},
{
"employeeNumber": "3109224",
"status": "A"
},
{
"employeeNumber": "3109514",
"status": "A"
},
{
"employeeNumber": "3109519",
"status": "A"
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/customers/1/employee-numbers?sortParam=employeeNumber&page=0&size=4&sort=employeeNumber,asc"
},
"prev": {
"href": "http://localhost:8080/customers/1/employee-numbers?sortParam=employeeNumber&page=0&size=4&sort=employeeNumber,asc"
},
"self": {
"href": "http://localhost:8080/customers/1/employee-numbers?sortParam=employeeNumber&page=1&size=4&sort=employeeNumber,asc"
},
"next": {
"href": "http://localhost:8080/customers/1/employee-numbers?sortParam=employeeNumber&page=2&size=4&sort=employeeNumber,asc"
},
"last": {
"href": "http://localhost:8080/customers/1/employee-numbers?sortParam=employeeNumber&page=3&size=4&sort=employeeNumber,asc"
}
},
"page": {
"size": 4,
"totalElements": 14,
"totalPages": 4,
"number": 1
}
}
推荐答案
我能够通过查看 http://izeye.blogspot.com/2015/01/consume-spring-data-rest-hateoas-hal.html 和
I was able to solved this by looking at http://izeye.blogspot.com/2015/01/consume-spring-data-rest-hateoas-hal.html and Why does RestTemplate not bind response representation to PagedResources?.
然而,这是我开发的代码,效果很好.
However, this is the code I developed which works very fine.
public class Demo {
private static RestTemplate restTemplate() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new Jackson2HalModule());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
converter.setObjectMapper(mapper);
return new RestTemplate(Collections.<HttpMessageConverter<?>> singletonList(converter));
}
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String URL = "http://localhost:8080/employee-api/employees/160166/employees?page=0&size=25";
RestTemplate restTemplate = restTemplate();
ResponseEntity<PagedResources<Resource<EmployeeDto>>> result = restTemplate.exchange(URL, HttpMethod.GET,
null/* httpEntity */, new ParameterizedTypeReference<PagedResources<Resource<EmployeeDto>>>() {});
PagedResources<Resource<EmployeeDto>> body = result.getBody();
ObjectMapper mapper = new ObjectMapper();
String writeValueAsString = mapper.writeValueAsString(body);
System.out.println(mapper.writeValueAsString(body));
}
}
这篇关于PagedResources<Resource 和 RestTemplate 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!