如何将对象类列表添加到arraylist?

时间:2019-01-03 07:51:05

标签: java spring spring-boot jackson

需要将具有错误的对象列表添加到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)

1 个答案:

答案 0 :(得分:0)

我将fun camelCase(stringToConvert: String): String { if (TextUtils.isEmpty(stringToConvert)) { return ""; } return Character.toUpperCase(stringToConvert[0]) + stringToConvert.substring(1).toLowerCase(); } 的类型更改为@ApiModelProperty(notes = "Список ошибок") List<ResponseError> errors; 不久,我添加了响应List<T> errors = new ArrayList<T>();产生的字段,以接收该字段并显示错误。

这解决了我的问题。感谢所有的建议。