使用jrs-rest-java-client将数据源注入到报表中

时间:2016-07-03 13:11:34

标签: rest jasper-reports jasperserver

我正在使用JasperReports Server及其jrs-rest-java-client API。当我访问远程服务器时,我想要注入报告应该使用的数据源(在我的情况下,它是一个数据库)。我不知道是否可以使用此API进行此操作。

1 个答案:

答案 0 :(得分:3)

是的,有可能。这是我在JasperReports Server v6.2.1中使用jrs-rest-java-client v6.2.3的方法:

// build configuration object
RestClientConfiguration configuration = new RestClientConfiguration("http://localhost:8080/jasperserver");
configuration.setContentMimeType(MimeType.JSON);
configuration.setAcceptMimeType(MimeType.JSON);
configuration.setAuthenticationType(AuthenticationType.SPRING);
configuration.setLogHttp(true);
configuration.setLogHttpEntity(true);

// build client and authenticate
JasperserverRestClient client = new JasperserverRestClient(configuration);
Session session = client.authenticate("jasperadmin", "jasperadmin");

String reportUnitUri = "/path/to/reportUnit";

// first get the version of the reportUnit to prevent update conflicts from optimistic locking
OperationResult<ClientReportUnit> reportUnitOperationResult = session.resourcesService().resource(reportUnitUri).get(ClientReportUnit.class);
Integer reportUnitVersion = reportUnitOperationResult.getEntity().getVersion();

// build patchDescriptor with the dataSource field
PatchDescriptor patchDescriptor = new PatchDescriptor();
patchDescriptor.setVersion(reportUnitVersion);
patchDescriptor.field("dataSource", "/path/to/repository/dataSource");

// apply the patchDescriptor
session.resourcesService().resource(reportUnitUri).patchResource(ClientReportUnit.class, patchDescriptor);