Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

java - PagedResources(PagedResources<Resource and RestTemplate does not works)

I am using Spring Boot REST example.

(我正在使用Spring Boot REST示例。)

In this I am using RestTemplate to call the endpoint which returns PagedResources<Resource<EmployeeDto>> Object.

(在此,我使用RestTemplate调用返回PagedResources<Resource<EmployeeDto>>对象的端点。)

But when calling through RestTemplate , I did not get any contents.

(但是,当通过RestTemplate调用时,我没有得到任何内容。)

However this service is build in another microservice which easily accessible over web and can be call through Postman.

(但是,此服务是内置在另一个微服务中的,该微服务可以通过Web轻松访问,并且可以通过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);

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
  }
}
  ask by PAA translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?

(我可以通过查看http://izeye.blogspot.com/2015/01/consume-spring-data-rest-hateoas-hal.html来解决此问题, 为什么RestTemplate不将响应表示绑定到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));
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...