How to convert collection to csv with jackson in java spring?


How to convert collection to csv with jackson in java spring?



I have a problem to convert a java.util.Collection to a csv file with jackson.



In the following code you can see a method to convert the collection to a csv-string.



But i need a method to convert the collection with com.fasterxml.jackson.
The Enum "DownloadType" get the column and headerlines for csv file.



Do you have an idea to fix them?


@RequestMapping(value = "/csv",
produces = {"text/csv"},
consumes = {"application/json"},
method = RequestMethod.POST)
public ResponseEntity<Object> exportCsv()
{
ResponseEntity<Object> response = null;
try
{
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, "text/csv; charset=UTF-8");
headers.add(HttpHeaders.CACHE_CONTROL, "no-store, must-revalidate");
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename="export.csv"");
headers.add(HttpHeaders.EXPIRES, "0");

byte csvBytes = null;
byte headerBytes = null;
byte lineBytes = null;
CsvMapper mapper = new
Collection<User> users = getUsers()
headerBytes = DownloadType.USER.getHeaderLine().getBytes("UTF-8");
lineBytes = mapper.writer(DownloadType.USER.getCsvSchema()).writeValueAsBytes(users);
if (headerBytes != null && lineBytes != null)
{
csvBytes = new byte[headerBytes.length + lineBytes.length];
System.arraycopy(headerBytes, 0, csvBytes, 0, headerBytes.length);
System.arraycopy(lineBytes, 0, csvBytes, headerBytes.length, lineBytes.length);
}
response = new ResponseEntity<>(csvBytes, headers, HttpStatus.OK);


}
catch (Exception e)
{
LOG.error(e.getMessage(), e);
}
return response;
}





What is the exact issue you're facing with this code? Is it that you can't use CsvMapper like you have it or is there an error?
– Joe W
Jul 3 at 9:56





The problem is, that i must not use the byte arrays. I must use the jackson libary to convert the collection to csv-file.
– MCW
Jul 3 at 9:59




1 Answer
1



Maybe try something like this. By writing the data directly to the servlet response the string will get returned directly back to the client as is without formatting or post-processing.


@RequestMapping(value = "/csv",
produces = {"text/csv"},
consumes = {"application/json"},
method = RequestMethod.POST)
public void exportCsv(HttpServletResponse response)
{
...
String headerString = DownloadType.USER.getHeaderLine()
String data = mapper.writer(DownloadType.USER.getCsvSchema()).writeValueAsString(users);
response.setContentType("text/plain; charset=utf-8");
response.getWriter().print(headerString);
response.getWriter().print(data);



Adapted from:
How to Return CSV Data in Browser From Spring Controller





Thank if i used the last 2 lines of your code, than i got a correct csv-string. This is better than my version of code.
– MCW
Jul 3 at 11:23







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages