Passing in content when generating JasperServer report using the REST API

后端 未结 3 1408
南旧
南旧 2021-02-08 10:32

I\'m working on a project that aims to replace our current PDF generator with JasperReports Server. The plan is to use the REST/HTTP API to reach a high level of abstraction bet

3条回答
  •  礼貌的吻别
    2021-02-08 11:02

    Even if I agree with the answer, witch states that the JasperServer has been built to fetch data by itself, I had still to pass the data trough the rest API because it's the legacy way of my company to build Jasper reports and because we want to use custom Java services to fetch data.

    I've found this described above to be the simpliest possible way to do this.

    Having this simple custom pojo that you want to pass to the report trough web API:

    public class CustomReport {
    
    private String content;
    
    public String getContent() {
        return content;
    }
    
    public void setContent(String content) {
        this.content = content;
    }
    
    public CustomReport() {
        super();
    }
    

    1) Define a custom jasper scriptlet that has to be deployed on the server as a resource related to the report, witch will deserialize the string into the custom pojo object using GSON:

    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) Use the parameter/variable with the custom scriptlet in jasper server:

    
        
    
    
    
    

    3) Invoke the API @ jasperserver/rest_v2/reportExecutions like this:

        "reportUnitUri" : "/report/Custom_report",
    "async":"false",
    "outputFormat":"pdf",
    "parameters" : {
        "reportParameter" : [
            {
                "name": "customReport",
                "value": ["{ \"content\" : \"test content\" } "]
            }
        ]
    }
    

提交回复
热议问题