即使我同意这个答案,witch 说 JasperServer 已经被构建为自行获取数据,我仍然必须通过其余 API 传递数据,因为这是我公司构建 Jasper 报告的传统方式,因为我们想要使用自定义 Java 服务来获取数据。
我发现上面描述的这是最简单的方法。
拥有您想要通过 Web API 传递给报告的简单自定义 pojo:
public class CustomReport {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public CustomReport() {
super();
}
1) 定义一个必须部署在服务器上的自定义 jasper scriptlet 作为与报告相关的资源,Witch 将使用 GSON 将字符串反序列化为自定义 pojo 对象:
public class CustomScriptlet
extends JRDefaultScriptlet { public void afterReportInit()
throws JRScriptletException
{
Object customSerializedObj = getParameterValue("customSerialized");
if (customSerializedObj != null)
{
String customSerializedStr = customSerializedObj.toString();
if ((customSerializedStr != null) && (customSerializedStr.length() > 0))
{
CustomReport customReport = new Gson().fromJson(customSerializedStr,
CustomReport.class);
setVariableValue("customReport", customReport);
}
}
}
2)使用参数/变量与碧玉服务器中的自定义脚本:
<scriptlet name="Scriptlet_1" class="eu.dedalus.jasper.api.scriptlet.CustomScriptlet">
<scriptletDescription><![CDATA[CustomScriptlet]]></scriptletDescription>
</scriptlet>
<parameter name="customSerialized" class="java.lang.String"/>
<variable name="customReport" class="com.test.CustomReport" calculation="System"/>
3)像这样调用API@jasperserver/rest_v2/reportExecutions:
"reportUnitUri" : "/report/Custom_report",
"async":"false",
"outputFormat":"pdf",
"parameters" : {
"reportParameter" : [
{
"name": "customReport",
"value": ["{ \"content\" : \"test content\" } "]
}
]
}