I used spring boot api. I created a api to export PDF file and use a jasper report. In the font-end section I used Angularjs. When I click the ExportPDF button and then I get the Error error message http / 1.1 405 Unable to download the file. I do not know that this error of font-end or back-end. It took me a long time for this. Please help me:
This is my code back-end : Controller
@RequestMapping(path = "/reports_statistic_orders", method = RequestMethod.GET)
public ResponseEntity<byte[]> reportsStatisticOrders(HttpServletRequest request) {
Map<String, Object> parameters = new HashMap<>();
String username = "Louis";
parameters.put("username", username);
byte[] bytes = jasperReportService.createPDF("report_order", parameters);
return ResponseEntity
.ok()
.header("Content-Type", "application/pdf; charset=UTF-8")
.header("Content-Disposition", "inline; filename=\"" + username + ".pdf\"")
.body(bytes);
}
This is my Repository :
@Autowired
ReportRepository reportRepository;
public byte[] createPDF(String inputFileName, Map<String, Object> params) {
return reportRepository.generatePDFReport(inputFileName, params);
}
Code implement :
private final StorageService storageService;
@Override
public byte[] generatePDFReport(String inputFileName, Map<String, Object> params) {
return generatePDFReport(inputFileName, params, new JREmptyDataSource());
}
@Override
public byte[] generatePDFReport(String inputFileName, Map<String, Object> params, JRDataSource dataSource) {
byte[] bytes = null;
JasperReport jasperReport = null;
try {
// Check if a compiled report exists
if (storageService.jasperFileExists(inputFileName)) {
jasperReport = (JasperReport) JRLoader
.loadObject(storageService.loadJasperFile(inputFileName));
}
// Compile report from source and save
else {
String jrxml = storageService.loadJrxmlFile(inputFileName);
jasperReport = JasperCompileManager.compileReport(jrxml);
}
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params,
dataSource);
bytes = JasperExportManager.exportReportToPdf(jasperPrint);
} catch (JRException e) {
throw new ApplicationException(APIStatus.ERR_LOAD_FILE, e);
}
return bytes;
}
FileSystemStorageService file :
@Component
public class FileSystemStorageService implements StorageService {
private final String folder = "/report";
@Override
public boolean jrxmlFileExists(String file) {
String tmpPath = folder + "/" + file + ".jrxml";
URL url = getClass().getClassLoader().getResource(tmpPath);
File reportfile;
try {
reportfile = new File(url.toURI().getPath());
System.out.println("PATH reportfile jasperFileExists: " + reportfile.getAbsolutePath());
System.out.println("PATH reportfile.getAbsolutePath() : " + reportfile.getAbsolutePath());
return true;
} catch (URISyntaxException ex) {
Logger.getLogger(FileSystemStorageService.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
@Override
public boolean jasperFileExists(String file) {
String tmpPath = folder + "/" + file + ".jasper";
URL url = getClass().getClassLoader().getResource(tmpPath);
File reportfile;
if (url == null) {
return false;
} else {
try {
reportfile = new File(url.toURI().getPath());
System.out.println("PATH jasperFileExists reportfile: " + reportfile);
System.out.println("PATH jasperFileExists reportfile.getAbsolutePath() : " + reportfile.getAbsolutePath());
} catch (URISyntaxException ex) {
Logger.getLogger(FileSystemStorageService.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
}
@Override
public String loadJrxmlFile(String file) {
String path = "";
String tmpPath = folder + "/" + file + ".jrxml";
URL url = getClass().getClassLoader().getResource(tmpPath);
try {
path = URLDecoder.decode(url.getPath(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new ApplicationException(APIStatus.ERR_STORAGE_FILE_NOT_FOUND, e);
}
return path;
}
@Override
public File loadJasperFile(String file) {
String tmpPath = folder + "/" + file + ".jasper";
URL url = getClass().getClassLoader().getResource(tmpPath);
File reportfile;
try {
reportfile = new File(url.toURI().getPath());
} catch (URISyntaxException e) {
throw new ApplicationException(APIStatus.ERR_STORAGE_FILE_NOT_FOUND, e);
}
return reportfile;
}
}
This is my code font end
$scope.exportPDFOrders = function (){
Util.createRequest('/statistics/reports_statistic_orders', function (response) {
var status = response.status;
if (status === 200) {
}
});
};
And show message 3 error :