在spring mvc中从ajax调用jasperreport时出现解析器错误
Posted
技术标签:
【中文标题】在spring mvc中从ajax调用jasperreport时出现解析器错误【英文标题】:parsererror when calling jasperreport from ajax in spring mvc 【发布时间】:2020-12-22 19:59:31 【问题描述】:我正在从 jquery ajax 调用端点以使用 spring mvc 生成 jasperreport,我想在新的浏览器选项卡中以 pdf 格式查看生成的报告。 问题是我收到报告解析器错误,从文本到应用程序/pdf 错误返回到 ajax 的转换失败,如果我从浏览器调试请求,我会看到响应中获得的 pdf 文本
这里是调用spring mvc方法的Ajax代码
var developerData = ;
developerData["from"] = $("#productivity_dateTimeFrom").val();
developerData["to"] = $("#productivity_dateTimeTo").val();
$.ajax(
type : "POST",
contentType : "application/json; charset=utf-8",
url : 'Productivity',
data : JSON.stringify(developerData),
dataType : 'application/pdf',
success : function(data)
window.open(data.fileUrl);
,
error : function(request,status,error)
alert(request)
alert(status)
alert(error)
);
要从 ajax 调用的 Spring mvc 方法:
@RequestMapping(value = "/Productivity", method = RequestMethod.POST, produces ="application/pdf")
@ResponseBody
public ResponseEntity<byte[]> runReport(@RequestBody String object) throws IOException, JRException
db_con db_con = new db_con();
Resource resource = new ClassPathResource("Productivity.jasper");
InputStream jasperStream = null;
Gson g = new Gson();
try
jasperStream = resource.getInputStream();
catch (IOException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
DateTimeRange p = g.fromJson(object, DateTimeRange.class);
Map<String, Object> params = new HashMap<String, Object>();
params.put("p_from_date",dateUtils.convertStringToTimestamp(p.getFrom().replace("T", " ")+":00.000"));
params.put("p_to_date",dateUtils.convertStringToTimestamp(p.getTo().replace("T", " ")+":00.000"));
params.put("username", "admin");
params.put("p2", 1L);
params.put("p3", "All");
JasperReport jasperReport = null;
try
jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
catch (JRException e)
// TODO Auto-generated catch block
e.printStackTrace();
Connection rep_connection = null;
try
rep_connection = db_con.get_connection();
catch (ClassNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
JasperPrint jasperPrint = null;
try
jasperPrint = JasperFillManager.fillReport(jasperReport, params, rep_connection);
catch (JRException e)
// TODO Auto-generated catch block
e.printStackTrace();
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
try
JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
catch (JRException e)
// TODO Auto-generated catch block
e.printStackTrace();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
String filename = "report.pdf";
headers.add("content-disposition", "inline;filename=" + filename);
return new ResponseEntity<>(outStream.toByteArray(), headers, HttpStatus.OK);
【问题讨论】:
【参考方案1】:尝试将 pdf 下载为 blob 并使用 blob url 进行查看
$.ajax(
type : "POST",
contentType : "application/json; charset=utf-8",
url : 'Productivity',
data : JSON.stringify(developerData),
xhrFields:
responseType: 'blob'
,
success : function(data)
window.open(URL.createObjectURL(data));
,
error : function(request,status,error)
alert(request)
alert(status)
alert(error)
);
【讨论】:
控制器方法是真的吗?以上是关于在spring mvc中从ajax调用jasperreport时出现解析器错误的主要内容,如果未能解决你的问题,请参考以下文章
尝试通过 Spring MVC 和 Thymeleaf 使用 React/Ajax 调用