返回并转换 ResponseEntity<List<T>>
Returning and Casting ResponseEntity<List<T>>
我正在尝试 return 一个 ResponseEntity
列表并将该响应投射到我的模型 类。
例如:如果我使用 ResponseEntity<List<ApplicationModel>>
它运行良好,但我不想为每个模型编写响应方法。
ResponseEntity
方法
public static <T> ResponseEntity<List<T>> getResponseList(String resourceURL) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<List<T>> entity = new HttpEntity<List<T>>(headers);
ResponseEntity<List<T>> response = restTemplate.exchange(resourceURL, HttpMethod.GET, entity,
new ParameterizedTypeReference<List<T>>() {
}, Collections.emptyMap());
return response;
}
方法调用
private final String url ="http://localhost:8090/xxx/application";
ResponseEntity<List<ApplicationModel> responseForApplications =
ResponseTemplate.getResponseList(url);
if (responseForApplications.getStatusCode() == HttpStatus.OK)
List<ApplicationModel> dtoApplications = responseForApplications.getBody();
我要投射的 JSON 响应示例
{"id":1,"name":"foo","description":"foo"}
错误
There was an unexpected error (type=Internal Server Error, status=500).
Error creating bean with name 'index': Invocation of init method failed; nested exception is java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.xxx.ApplicationModel
The issue's coming from Jackson. When it doesn't have enough
information on what class to deserialize to, it uses LinkedHashMap.
Since you're not informing Jackson of the element type of your
ArrayList, it doesn't know that you want to deserialize into an
ArrayList of ApplicationModels. So it falls back to the default.
Instead, you could probably use as(JsonNode.class), and then deal with
the ObjectMapper in a richer manner than rest-assured allows.
更多信息请参考。
使用 mapper.convertValue
适合我。
ObjectMapper mapper = new ObjectMapper();
List<ApplicationModel> dtoApplications = mapper.convertValue(responseForApproles.getBody(), new TypeReference<List<ApplicationModel>>() {});
我正在尝试 return 一个 ResponseEntity
列表并将该响应投射到我的模型 类。
例如:如果我使用 ResponseEntity<List<ApplicationModel>>
它运行良好,但我不想为每个模型编写响应方法。
ResponseEntity
方法
public static <T> ResponseEntity<List<T>> getResponseList(String resourceURL) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<List<T>> entity = new HttpEntity<List<T>>(headers);
ResponseEntity<List<T>> response = restTemplate.exchange(resourceURL, HttpMethod.GET, entity,
new ParameterizedTypeReference<List<T>>() {
}, Collections.emptyMap());
return response;
}
方法调用
private final String url ="http://localhost:8090/xxx/application";
ResponseEntity<List<ApplicationModel> responseForApplications =
ResponseTemplate.getResponseList(url);
if (responseForApplications.getStatusCode() == HttpStatus.OK)
List<ApplicationModel> dtoApplications = responseForApplications.getBody();
我要投射的 JSON 响应示例
{"id":1,"name":"foo","description":"foo"}
错误
There was an unexpected error (type=Internal Server Error, status=500). Error creating bean with name 'index': Invocation of init method failed; nested exception is java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.xxx.ApplicationModel
The issue's coming from Jackson. When it doesn't have enough information on what class to deserialize to, it uses LinkedHashMap.
Since you're not informing Jackson of the element type of your ArrayList, it doesn't know that you want to deserialize into an ArrayList of ApplicationModels. So it falls back to the default.
Instead, you could probably use as(JsonNode.class), and then deal with the ObjectMapper in a richer manner than rest-assured allows.
更多信息请参考
使用 mapper.convertValue
适合我。
ObjectMapper mapper = new ObjectMapper();
List<ApplicationModel> dtoApplications = mapper.convertValue(responseForApproles.getBody(), new TypeReference<List<ApplicationModel>>() {});