使用 jrs-rest-java-client 将数据源注入报告

Inject a datasource using jrs-rest-java-client to a report

我正在使用 JasperReports Server 及其 jrs-rest-java-client API。当我访问远程服务器时,我想注入报告应该使用的数据源(在我的例子中是一个数据库)。我不知道是否可以使用这个 API 来做到这一点。

是的,这是可能的。这就是我在 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);