【发布时间】:2019-07-20 17:06:08
【问题描述】:
这是我的 get API 返回的员工列表。
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/get")
public List<Employee> getEmployee() {
return employeeFacade.getAllEmployee();
}
我想从ResponseEntity<String> 中提取Employee 对象。
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
//headers.setContentType(MediaType.APPLICATION_JSON);
Employee employee = new Employee();
HttpEntity<Employee> entity = new HttpEntity<Employee>(employee, headers);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
System.out.println("result:" + result);
如何做到这一点?
【问题讨论】:
-
你的问题是......?
-
我们可以看看你的响应json吗?
-
你为什么用
String.class是你想要的是一个Employee,而不是一个String? -
如果我使用的是employee.class,它会出错,因为我想提取员工列表而不是单个员工。抱歉没有提及
-
你可以通过'new ParameterizedTypeReference
- >() {}'
标签: java spring spring-mvc jersey-2.0