0

I have Jersey endpoint that is attempting to create a CSV file and return it via GET; however, all these attempts end up with the same result - the ok status (200) is returned but no file.

    @GET
    @Path("/research")
    @Produces(MediaType.TEXT_PLAIN)
    public Response dumpAllResearchAndSupportData() {
        QuickDumpBuilderService dumpBuilderService = getQuickDumpService();

        List<DevelopmentProposal> researchOtherProposals = dumpBuilderService.getAllResearchRecords();

        String csvLocation = dumpBuilderService.buildCSV(researchOtherProposals);

        File file = new File(csvLocation);
        String filename = "reserach_" + UUID.randomUUID().toString() + ".csv";
        return Response.ok(file, MediaType.TEXT_PLAIN).header("Content-Disposition", "attachment; filename=" + filename).build();
    }

My CSV file is properly created, but it fails to be returned along with the response. Notice above, I'm writing the CSV to a temporary location in my tomcat folder and then passing the path to that file back and then attempting to read that from the location here.

Another attempt with the same result where instead of writing the CSV to temp location, I'm just trying to write the ByteArrayOutputStream to the response object.

    @GET
    @Path("/research")
    @Produces(MediaType.APPLICATION_JSON)
    public Response dumpAllResearchAndSupportData() {
        QuickDumpBuilderService dumpBuilderService = getQuickDumpService();
        // Step 1.  Retrieve all research and other proposals
        List<DevelopmentProposal> researchOtherProposals = dumpBuilderService.getAllResearchRecords();
        // Step 2.  Create CSV File
        ByteArrayOutputStream csvBaos = dumpBuilderService.buildCSV(researchOtherProposals);
        // Step 3.  Create spreadsheet
        ByteArrayOutputStream excelBaos = dumpBuilderService.createExcelSpreadsheet(csvBaos, servlet);
        // Step 4.  Return spreadsheet
        Response.ResponseBuilder response = Response.ok(excelBaos);
        return response.build();

Another attempt where I added this "writer" into the response. This attempt generated an error that a "MessageBodyWriter for the ByteArrayStream was not found." That prompted the attempt below.

    @GET
    @Path("/research")
    @Produces(MediaType.TEXT_PLAIN)
    public Response dumpAllResearchAndSupportData() {
        ....
        // Step 4.  Return spreadsheet
        return Response.ok(getOut(csvBaos.toByteArray())).build();
    }

    private StreamingOutput getOut(final byte[] csvBytes) {
        return new StreamingOutput() {
            @Override
            public void write(OutputStream out) throws IOException, WebApplicationException {
                out.write(csvBytes);
                out.flush();
            }
        };
    }

I've looked at the following similar answers, and I've attempted all of them with no success. Not sure what I'm doing wrong - I suspect that it's something to do with how I setup my endpoint and defer to the Java REST API experts here.

File download/save from a Jersey rest api call using ANGULARJS . file is attached with response as "Content-Disposition"

Download CSV file via Rest

jersey - StreamingOutput as Response entity

Thanks for your help.

ivan_drago
  • 113
  • 5
  • https://stackoverflow.com/questions/52118550/generate-a-csv-file-using-jersey-restful-webservice - check this answer – Govil Kumar Feb 17 '23 at 03:47

0 Answers0