admin管理员组

文章数量:1278883

@Override
public void configure(){
   rest("/camel/loss-events")
   .get("/csv-download")
    .to("direct:generateCsv");
    CsvDataFormat csv = new CsvDataFormat();
    csv.setDelimiter(getCsvParameterDelimiter().charAt(0));
    from("direct:generateCsv")
            .process(exchange -> {
                List<LossEvent> lossEventList=   Optional.ofNullable(bean.getLossEventEList()).orElse(Collections.emptyList());
                List<String[]> csvData = new ArrayList<>();
                csvData.add(getHeaders());
                csvData.addAll(convertLossEventsToCsvRows(lossEventList));
                exchange.setProperty("csvData", csvData);
                exchange.getIn().setBody(csvData);
            })
            .marshal().csv()
            .setHeader("Content-Type", constant("text/csv; charset=UTF-8"))
            .setHeader("Content-Disposition", constant("attachment; filename=Loss_Events.csv"))
            .to("stream:out");
}

I tried to generate the csv file based on a certain delimiter that i retrieve from my database. I debugged and the csvParameterDelimiter() retrieves a semicolon ';' of char type which is what i want. When i marshal it seems that the default attributes of CsvDataFormat are set instead of the ones i am trying to set.

@Override
public void configure(){
   rest("/camel/loss-events")
   .get("/csv-download")
    .to("direct:generateCsv");
    CsvDataFormat csv = new CsvDataFormat();
    csv.setDelimiter(getCsvParameterDelimiter().charAt(0));
    from("direct:generateCsv")
            .process(exchange -> {
                List<LossEvent> lossEventList=   Optional.ofNullable(bean.getLossEventEList()).orElse(Collections.emptyList());
                List<String[]> csvData = new ArrayList<>();
                csvData.add(getHeaders());
                csvData.addAll(convertLossEventsToCsvRows(lossEventList));
                exchange.setProperty("csvData", csvData);
                exchange.getIn().setBody(csvData);
            })
            .marshal().csv()
            .setHeader("Content-Type", constant("text/csv; charset=UTF-8"))
            .setHeader("Content-Disposition", constant("attachment; filename=Loss_Events.csv"))
            .to("stream:out");
}

I tried to generate the csv file based on a certain delimiter that i retrieve from my database. I debugged and the csvParameterDelimiter() retrieves a semicolon ';' of char type which is what i want. When i marshal it seems that the default attributes of CsvDataFormat are set instead of the ones i am trying to set.

Share Improve this question asked Feb 24 at 10:01 user28803304user28803304 1
Add a comment  | 

1 Answer 1

Reset to default 0

Looking at your method calls, I think you are not passing in your csv object with the correct delimeter being set to the marshall call. Take a look at

...
.marshal().csv(). // this is very well creating a new default instance
...

This should be changed to something like

...
marshal(csv). # the name of your CSVDataFormat instance
...

so that the marshaller uses your csv instance

本文标签: