Spring REST - пропустить PDF через запрос - PullRequest
0 голосов
/ 22 января 2020

У меня есть 2 приложения Spring Boot ... Первое приложение должно сгенерировать PDF и вернуть его ... Второе приложение вызывает первое приложение, чтобы получить PDF и вернуть его пользователю ...

Здесь я генерирую PDF.

@RequestMapping(value = "/html2pdf", method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource> report(@RequestParam() String htmlContent) {

        try {
            ByteArrayInputStream pdfDocument = pdfGenerator.generatePDF(htmlContent);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_PDF);
            headers.add("Content-Disposition", "inline; filename=generated.pdf");

            return ResponseEntity
                    .ok()
                    .headers(headers)
                    .body(new InputStreamResource(pdfDocument));
        } catch (IOException e) {
            return ResponseEntity.badRequest().build();
        }
    }

Здесь я пытаюсь позвонить против первого приложения и вернуть PDF ...

@RequestMapping(value = "/pdfreport", method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource> report() {

        ResourceHttpMessageConverter resourceHttpMessageConverter = new ResourceHttpMessageConverter();

        List<MediaType> supportedApplicationTypes = new ArrayList<>();
        MediaType pdfApplication = new MediaType("application", "pdf");
        supportedApplicationTypes.add(pdfApplication);

        resourceHttpMessageConverter.setSupportedMediaTypes(supportedApplicationTypes);
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        messageConverters.add(resourceHttpMessageConverter);

        RestTemplate pdfGenerator = new RestTemplate();
        pdfGenerator.setMessageConverters(messageConverters);

        ResponseEntity<InputStreamResource> response = pdfGenerator.getForEntity("http://localhost:1080/pdf-generator/html2pdf?htmlContent=<h2>HUHU</h2>", InputStreamResource.class);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "inline; filename=generated.pdf");
        headers.setContentType(MediaType.APPLICATION_PDF);

        return ResponseEntity
                .ok()
                .headers(headers)
                .body(response.getBody());
    }

в этом созвездии я получаю следующую ошибку:

java.io.IOException: stream is closed

Я уже пробовал его с ResponseExtractor ...

В чем здесь проблема? Как я могу решить это проще?

Спасибо

1 Ответ

0 голосов
/ 23 января 2020

InputStreamResource Должен использоваться только в том случае, если не указано другое c Реализация ресурса не применима. В частности, предпочитайте ByteArrayResource или любую из реализаций ресурсов на основе файлов, где это возможно.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/io/InputStreamResource.html

...