2017-02-17 76 views
0

我有一個ExtJS應用程序,它顯示使用REST服務的彈簧引導應用程序的PDF。到目前爲止,這工作正常,但是當我從1.3 - > 1.4更新了spring-boot版本時,代碼無法正常工作,並顯示爲空白PDF作爲響應。從Spring-Boot應用程序在Sencha ExtJs中打印遠程pdf不起作用

這是我的代碼:

ExtJS的 - 煎茶

Ext.Ajax.request({ 
    url: MyApp.Application.Globals.baseUrl + url, 
    params: params, 
    method: 'POST', 
    async: false, 
    headers:{ 
     'Authorization': Utils.getAuthorization() 
    }, 
    scope : this, 
    // ON SUCCESS 
    success: function(response) { 
     window.open('data:application/pdf,' + escape(response.responseText)); 
    }, 
    // ON FAILURE 
    failure: function(err) { 
     console.log(err); 
    } 
}); 

服務器代碼(Spring的引導)

String report = reportService.executeReport(....); 

response.setContentType("application/pdf"); 

try { 
    response.getWriter().write(report); 
} catch (IOException e) { 
    ELogger.error(this, CoreConstants.LOGGER_CATEGORY, "error creating pdf", e); 
} 

POM

<spring-boot.version>1.4.4.RELEASE</spring-boot.version> with 1.3.3 it works fine 

如果有人能幫助我, 我會很感激。

問候!

+0

最後我發現workaroung,我有修改對象輸入反應,現在我有一個 –

回答

0

我已經發現使用ResponseEntity作爲響應變通方法:

@RequestMapping(value = "/example", produces = "application/pdf") 
public ResponseEntity<InputStreamResource> exampleMethod(){ 

    HttpHeaders headers = new HttpHeaders(); 
    headers.setContentType(MediaType.parseMediaType("application/pdf")); 
    headers.add("Access-Control-Allow-Origin", "*"); 
    headers.add("Access-Control-Allow-Methods", "GET, POST, PUT"); 
    headers.add("Access-Control-Allow-Headers", "Content-Type"); 
    headers.add("Content-Disposition", "filename=sysparamspdf"); 
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); 
    headers.add("Pragma", "no-cache"); 
    headers.add("Expires", "0"); 

    headers.setContentLength(report.getBytes(StandardCharsets.UTF_8).length); 
    ResponseEntity<InputStreamResource> response = new ResponseEntity<InputStreamResource>(
      new InputStreamResource(stream), headers, HttpStatus.OK); 
    return response; 

}