[英]how do i convert my json response to a txt tab delimited file rest api java?
I have a sql query.我有一个 sql 查询。
this is my abc.class这是我的 abc.class
public class Abc {
private Integer ABC_ID;
private String RECIP_ID, RECIP_FIRST_NAME, RECIP_MIDDLE_NAME;
public Abc() {
//super();
// TODO Auto-generated constructor stub
}
}
and the rest are the getters and setters;其余的是 getter 和 setter;
@RequestMapping(value = "/abc/export", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<List<abc>> getProviderExport(@RequestParam String id, HttpServletResponse response){
List<abc> abcdetails = jdbcTemplate.query(sqlQuery, new BeanPropertyRowMapper<abc>(abc.class));
HttpHeaders responseHeaders = new HttpHeaders();
//responseHeaders.setContentType(MediaType.TEXT_PLAIN);
String filename1 = "output.txt";
//responseHeaders.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
responseHeaders.add("Content-Disposition","attachment; filename="+filename1);
return new ResponseEntity<>(abcdetails, responseHeaders, HttpStatus.OK);
}
when I execute the above code and run the api, my api prompts to save the output file as output.txt, but when i open the output file, the content is still in json format.当我执行上述代码并运行api时,我的api提示将输出文件保存为output.txt,但是当我打开输出文件时,内容仍然是json格式。 can anyone help me on how to achieve the output.txt file that is using tab delimited and uppercase.
任何人都可以帮助我如何实现使用制表符分隔和大写的 output.txt 文件。
Because you return ResponseEntity<List<abc>>
.因为您返回
ResponseEntity<List<abc>>
。 A List<>
is converted automatically to array JSON. List<>
会自动转换为数组 JSON。 You need to return a ResponseEntity<String>
.您需要返回一个
ResponseEntity<String>
。 So, convert your List<abc>
to String
.因此,将您的
List<abc>
转换为String
。
Something like this :像这样的事情:
final StringBuilder textData = new StringBuilder();
for (final Abc abcDetail : abcdetails) {
final String line = abcDetail.getRECIP_FIRST_NAME() + " " + abcDetail.getRECIP_ID() + " ";//You can add your other field
textData.append(line).append("\n");
}
return new ResponseEntity<>(textData.toString(), responseHeaders, HttpStatus.OK);
You have to convert that json format to text format using Jackson JSON Java Parser I am attaching a link for a brief example on how to convert jsno to txt file with tab delimited and uppercase.您必须使用 Jackson JSON Java Parser 将该 json 格式转换为文本格式我附上了一个关于如何将 jsno 转换为带有制表符分隔和大写的 txt 文件的简短示例的链接。
Here is the link : https://www.journaldev.com/2324/jackson-json-java-parser-api-example-tutorial这是链接: https : //www.journaldev.com/2324/jackson-json-java-parser-api-example-tutorial
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.