Spring and Jasper - Browser does not understand streaming for download

1

I have an application that uses version 6.2.0 with version 3.2.14, , 8 and on the front end we use angularjs . The requests are made via ajax .

Spring perfectly understands the object that comes in the URL and the report seems to be generating the right way as well, as we test passing a malformed JSON and we naturally make an exception.

The problem is that when returning to the browser, the request is not interpreted in any way to download the file and spits only the contents of the file, even without the header that jasper-reports generates for PDF files .

When I write the contents of the byte vector in the OutputStream HttpServletResponse the header of the file exits as I can see in the response that Chrome shows in the console. This part of the code is visible in the code below the method, but commented.

Method that receives the request:

@RequestMapping(value = RequisicaoPoRestService.ROOT_MAPPING_CIA_REQUISICOES_PO + "/pdfdetalhepedido", method = RequestMethod.GET)
@ResponseBody
@Secured({ "ROLE_CONSULTARPO" })
public ResponseEntity<byte[]> gerarPDFDetalhePedido(final HttpServletResponse response, @PathVariable(RequisicaoPoRestService.ID_CIA) final Long idCia,
        @RequestParam("detelhePedidoJson") final String detelhePedidoJson) {
    // Parametrizar via properties.
    final String caminhoTemplate = "C:\Projetos\wasp\wasp-api\src\main\webapp\WEB-INF\pdftemplates\Blank_A4.jasper";
    final byte[] relatorio = PdfUtils.gerarPDFViaJSON(caminhoTemplate, detelhePedidoJson, null);

    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    headers.setCacheControl("must-revalidate, no-cache, no-store");
    headers.add("Cache-Control", "post-check=0, pre-check=0");
    headers.setPragma("no-cache");
    headers.setDate(0);
    headers.setContentLength(relatorio.length);
    headers.setExpires(0);
    headers.setLastModified(0);
    headers.set("Content-Disposition", "attachment; filename=DetalhePedido.pdf");

    // response.reset();
    // response.setHeader("Content-Disposition", "inline; filename=" +
    // filename);
    // response.setContentType("application/pdf");
    // response.setContentLength(relatorio.length);
    return new ResponseEntity<>(relatorio, headers, HttpStatus.OK);
}

Method that generates the report

@SuppressWarnings("unused")
public static byte[] gerarPDFViaJSON(final String caminhoDoTemplateDeNFe, final String json, final String logo) {
    try (final ByteArrayInputStream stream = new ByteArrayInputStream(json.getBytes())) {

        // Fonte de Dados.
        final JsonDataSource ds = new JsonDataSource(stream);

        // Parâmetros
        final Map<String, Object> params = new HashMap<>();
        // params.put("Logo", logo);

        // Gerando o relatório
        final JasperPrint print = JasperFillManager.fillReport(caminhoDoTemplateDeNFe, params, ds);

        // Exportando em pdf.
        final byte[] relatorio = JasperExportManager.exportReportToPdf(print);
        return Arrays.copyOf(relatorio, relatorio.length);
    } catch (final JRException | IOException e) {
        PdfUtils.LOGGER.error(e.toString());
        return null;
    }
}

Any suggestion of the reason for the request not being interpreted to be downloaded from the file, even though I am cleaning the cache, setting it on the header of the file that is a downloadable file, its size and so on?

    
asked by anonymous 01.12.2015 / 18:25

1 answer

0

I discovered after a lot of research, trial and error that the problem was not in the code, but rather in my forgetting that requests return only text, meaning it is impossible to return an array of bytes.

Made the call with a "common" get instead of using calls that are always via Ajax everything went live.

    
04.12.2015 / 13:19