【发布时间】:2019-05-29 19:21:37
【问题描述】:
需要将有错误的对象列表添加到 arraylist 主体。
我试图将response.getErrors() 设置为response.getBody() 的代码:
String jsonResponse = restTemplate.postForObject(url, requestBody, String.class);
JavaType valueType = mapper.getTypeFactory().constructParametricType(ResponseContainer.class, type);
ResponseContainer<ResponseType> response = mapper.readValue(jsonResponse, valueType);
// TODO if error != null put errors to body
if (response.getErrors() != null) {
response.getBody().addAll(response.getErrors());
}
return response.getBody();
模型类:
@ApiModelProperty(notes = "Список параметров сообщения")
List<T> body = new ArrayList<T>();
@ApiModelProperty(notes = "Список ошибок")
List<ResponseError> errors;
错误是:
Error:(70, 35) java: no suitable method found for add(java.util.List<com.infin.it.integrator.impl.base.ResponseError>)
method java.util.Collection.add(ResponseType) is not applicable
(argument mismatch; java.util.List<com.infin.it.integrator.impl.base.ResponseError> cannot be converted to ResponseType)
method java.util.List.add(ResponseType) is not applicable
(argument mismatch; java.util.List<com.infin.it.integrator.impl.base.ResponseError> cannot be converted to ResponseType)
【问题讨论】:
-
这真的是完整的错误信息吗?
-
是的,完全错误在 response.getErrors()
-
不,我的意思是“在列表中无法应用”完整的错误消息?
-
哦,对不起。错误是:无法将 List 中的 addAll (java.util.Collection extends ResponseType>) 应用于 (java.util.List
) -
看起来 com.infin.it.integrator.impl.base.ResponseError 没有从 ResponseType 继承。如果这两个列表都是 ´
response对象的一部分,为什么你还需要加入它们?
标签: java spring spring-boot jackson