Home > Mobile >  how do i convert my json response to a txt tab delimited file rest api java?
how do i convert my json response to a txt tab delimited file rest api java?

Time:10-28

I have a sql query.

    @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. can anyone help me on how to achieve the output.txt file that is using tab delimited and uppercase.

CodePudding user response:

Because you return ResponseEntity<List<abc>>. A List<> is converted automatically to array JSON. You need to return a ResponseEntity<String>. So, convert your List<abc> to String.

CodePudding user response:

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.

Here is the link : https://www.journaldev.com/2324/jackson-json-java-parser-api-example-tutorial

  • Related