如何编写列表< number>使用OpenCSV?

发布于 2025-01-28 02:41:06 字数 1313 浏览 4 评论 0原文

我在一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2025-02-04 02:41:06

尝试在要映射的每个字段上使用csvbindbyname(指定注释的列属性不是强制性的)。

您甚至可以使用csvbindbyposition(如果需要)。

Try using CsvBindByName on every field you want to map (specify the column attribute of the annotation is not mandatory).

You can even use CsvBindByPosition if you prefer.

小兔几 2025-02-04 02:41:06

你试图改变吗?

 beanToCSV.write(item); -> beanToCSV.writeNext(item);

或者

    for(StacItem item : items){
        beanToCSV.write(item);
    }

    // to

    beanToCSV.write(items);

Did you try to change ?

 beanToCSV.write(item); -> beanToCSV.writeNext(item);

or

    for(StacItem item : items){
        beanToCSV.write(item);
    }

    // to

    beanToCSV.write(items);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文