休息客户Templat查找列表客户

时间:2016-07-25 19:37:30

标签: java spring rest

  • 在Spring 4.2.1-Release
  • 使用RestTemplate进行访问休息
  • 创建此
@Bean
@Scope("prototype")
@Autowired
public RestTemplate restTemplate(
    @Qualifier("httpComponentsClientHttpRequestFactory") HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory,
    @Qualifier("mappingJackson2HttpMessageConverter") MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter)
{
    final RestTemplate restTemplate = new RestTemplate(httpComponentsClientHttpRequestFactory);
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(new FormHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
    messageConverters.add(mappingJackson2HttpMessageConverter);
    restTemplate.setMessageConverters(messageConverters);
    return restTemplate;
}

在JUnit测试中使用injec后

HttpHeaders requestEntity = new HttpHeaders();
requestEntity.setAccept(Collections.singletonList(new MediaType("application","json")));
HttpEntity<?> entity = new HttpEntity<Customer>(requestEntity);
ResponseEntity<Customer> responseEntity = restTemplate.exchange(uriFound.toUri(), HttpMethod.GET, entity, Customer.class);
List<Customer> customers = (List<Customer>) responseEntity.getBody();

列表返回null, 当使用其余的SoapUI客户端返回

{
    "totalItems": 4,
    "items": [{
        "id": "4052BF1857789C1A",
        "email": "test@test.com"
    }, {
        "id": "4052BF1957789C1A",
        "email": "test2@test.com"
    }, {
        "id": "4052BF2257789C1A",
        "email": "test3@test.com"
    }, {
        "id": "4052BF2157789C1A",
        "email": "test4@test.com"
    }]
}

关于如何以这种方式休息的提示?

0 个答案:

没有答案