Say I have CSV data in a row and you want to return it from a Spring controller. Imagine the data looks like this:
a,b,c 1,2,3 4,5,6
No matter what I tried, the lines of the newline look literally "\ n" in the content of the response, and if I double them, as in "\ n", the answer simply contains double backslashes. In general, how can I return text data with new lines in it without changing new lines? I know how to return plain text, but nonetheless, the content comes with escaped newline characters ... This is what I have (using Spring 3.0.5, not optional)
@RequestMapping(value = "/api/foo.csv") public ResponseEntity<String> fooAsCSV() { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add("Content-Type", "text/plain; charset=utf-8"); String data = "a,b,c\n1,2,3\n3,4,5"; return new ResponseEntity<>(data, responseHeaders, HttpStatus.OK); }
What literally produces a string
"a,b,c\n1,2,3\n,3,4,5"
In the browser. How to make it create the correct data with new lines in a measure as shown above?
java spring spring-mvc csv
David Williams
source share