4

バックエンドで生成された PDF ドキュメントがあります。Spring MVC REST フレームワークを使用してこれを返したい。MarshallingView と ContentNegotiatingViewResolver はどのように見えるべきですか?

私が見つけたサンプルに基づいて、コントローラーはこれを返します:

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

-ありがとうございました。

4

1 に答える 1

16

HttpServletRequest次のように、メソッドを明示的に取り込んでHttpServletResponseHttpServletResponse に直接ストリーミングするように定義できます。

@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);
    }
}
于 2012-08-03T22:41:25.273 に答える