如何编写列表< number>使用OpenCSV?
我在一个stacitem对象中有以下字段:
@JsonProperty
private List<Number> bbox = null;
我使用OpenCSV进行了基本实现,将此对象写入CSV,并且主要与此代码一起使用(我只是显示相关部分):
final StatefulBeanToCsv<Object> beanToCSV = new StatefulBeanToCsvBuilder<>(writer)
.withSeparator(';')
.build();
for(StacItem item : items){
beanToCSV.write(item);
}
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.CONTENT_TYPE, "text/csv");
httpHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=exportItems.csv");
logger.info("END WRITING");
return new ResponseEntity<>(new FileSystemResource(file), HttpStatus.OK);
这是扭曲!在我的微服务的日志中,我看到了那个ottacitem的完整结构,它应该具有此Bbox字段:
bbox=[8.24275148213394, 45.5050129344147, 7.62767704092889, 45.0691351737573]
虽然我的实现仅返回了这一点:
"8.24637830863774"
因此,当我打开CSV时,我刚刚找到了一个具有一个值的列“ Bbox”,但我也需要其他人..您能告诉我为什么它停在第一个或如何获得其他人3?
更新: 我发现这可以解决问题!但是然后...它仅针对每个障碍的单个字段导出,因此我失去了对象中的其他所有字段。
@CsvBindAndSplitByName(elementType = Number.class, writeDelimiter = ",")
@JsonProperty("bbox")
private List<Number> bbox = null;
谢谢
I have the following field inside a StacItem Object:
@JsonProperty
private List<Number> bbox = null;
I made a basic implementation with OpenCSV to write this Object into a CSV, and it mostly works with this code (i'm showing just the relevant part):
final StatefulBeanToCsv<Object> beanToCSV = new StatefulBeanToCsvBuilder<>(writer)
.withSeparator(';')
.build();
for(StacItem item : items){
beanToCSV.write(item);
}
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.CONTENT_TYPE, "text/csv");
httpHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=exportItems.csv");
logger.info("END WRITING");
return new ResponseEntity<>(new FileSystemResource(file), HttpStatus.OK);
Here's the twist! In the logs of my Microservice, I see the full structure of that StacItem, and it should have this bbox field:
bbox=[8.24275148213394, 45.5050129344147, 7.62767704092889, 45.0691351737573]
While my implementation returns just this:
"8.24637830863774"
So when I open my CSV I just found the column "bbox" with one value but I need the others too..Can you please tell me why it stops on the first one or how to get the others 3?
UPDATE:
I found that this does the trick! But then...it exports just this single field for every StacItem so I lose every other field in my Object.
@CsvBindAndSplitByName(elementType = Number.class, writeDelimiter = ",")
@JsonProperty("bbox")
private List<Number> bbox = null;
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在要映射的每个字段上使用
csvbindbyname
(指定注释的列属性不是强制性的)。您甚至可以使用
csvbindbyposition
(如果需要)。Try using
CsvBindByName
on every field you want to map (specify thecolumn
attribute of the annotation is not mandatory).You can even use
CsvBindByPosition
if you prefer.你试图改变吗?
或者
Did you try to change ?
or