java - OpenCSV + RestTemplate : Array-Types

标签 java spring opencsv

我正在尝试使用 Spring 的 RestTemplate 将远程 CSV 文件解析为 bean。我想使用 RestTemplate 的原因是它已经解决了 Http-Connection 的所有低级问题(主要是资源管理),并且我可以轻松地使用它设置超时。

因此,我编写了一个自定义 HttpMessageConverter,它使用 OpenCSV 将 HttpMessage 转换为 CSV bean。该 bean 使用 OpenCSV 的相应 CsvToBean 注释进行注释。然而,问题是 RestTemplate 为您提供的正是您在类参数中指定的内容。

restTemplate.exchange("www.exmample.com", HttpMethod.GET, null, MyDTO.class)

以上代码将始终返回 ONE MyDTO。如果您想要 MyDTO 列表,则使用 RestTemplate 您必须指定它:

restTemplate.exchange("www.exmample.com", HttpMethod.GET, null, MyDTO[].class)

然而,OpenCSV 的工作方式有所不同。

final CsvToBeanBuilder<MyDTO> beanBuilder = new CsvToBeanBuilder<>(new InputStreamReader(httpInputMessage.getBody()));
beanBuilder.withType(MyDTO.class); // not sure of this is needed
beanBuilder.build().parse(); // returns List<MyDTO>

因此,OpenCSV 采用 DTO 的单一非数组版本,其解析全部函数返回给定内容的列表。问题是我在自定义 HttpMessageConverter 中使用 OpenCSV。所以我被迫使用从 RestTemplate 获得的类类型:

// Inside of my class that extends HttpMessageConverter<T>

@Override
public T read(final Class<? extends T> aClass, final HttpInputMessage httpInputMessage) 
    throws IOException, HttpMessageNotReadableException {

  final CsvToBeanBuilder<T> beanBuilder = new CsvToBeanBuilder<>(new InputStreamReader(httpInputMessage.getBody()));
  beanBuilder.withType(aClass); // This throws an exception if aClass is of MyDTO[].class. 
                                // The exception states that there is no way to init the class.
  // This returns a List<Class<T>> which is already wrong. This findFirst() workaround could work but it fails earlier
  return beanBuilder.build().parse().stream().findFirst().orElse(null);
}

我能想到的唯一解决方案是将我真正想要的类硬编码到我的自定义 HttpMessageConverter 中。我想避免这种情况,因为这样这个消息转换器对于项目的其余部分就没有可重用性。 这个问题还有其他解决方案吗?

最佳答案

您将需要使用 ParameterizedTypeReference。这样您就可以将其解析为 ParameterizedTypeReference>。

关于java - OpenCSV + RestTemplate : Array-Types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60940510/

相关文章:

java - 从 CSV 字符串而不是文件解析

java.lang.ClassNotFoundException : android. 支持.v4.view.TintableBackgroundView

java - 如果我可以用另一种方式初始化实例变量,为什么还要使用构造函数

Spring MVC 与 Spring WebFlow

java - 如何将多个列表中的值写入新的 csv 文件?

java - 打开 CSV : How to escape comma while exporting SQL dump to CSV

java - 在 java String ReplaceAll 方法中未给出预期结果

java - 在 Java 中将两个位集交叉成一个新的位集的最快方法是什么?

java - 向表中插入值时出现错误

java - 获取 Struts2 中已配置操作的列表