Spring 3.0 Java REST returns PDF document

I have a PDF created in the backend. I want to return this using the Spring MVC REST framework. What do MarshallingView and ContentNegotiationViewResolver look like?

Based on the sample found, the controller will have this as a return:

return new ModelAndView(XML_VIEW_NAME, "object", byteArrayResponseContainingThePDFDocument); 

-to you.

+4
source share
1 answer

You can define your method for expressing HttpServletRequest and HttpServletResponse and pass it directly to HttpServletResponse, as follows:

 @RequestMapping(value="/pdfmethod", produces="application/pdf") public void pdfMethod(HttpServletRequest request, HttpServletResponse response){ response.setContentType("application/pdf"); InputStream inputStream = null; OutputStream outputStream = null; try{ inputStream = getInputStreamFromYourPdfFile(); outputStream = response.getOutputStream(); IOUtils.copy(inputStream, outputStream); }catch(IOException ioException){ //Do something or propagate up.. }finally{ IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } } 
+16
source

All Articles