尝试使用/jrs-rest-java-client
上传报告单位时,我遇到了一个错误的请求。
我一直在JasperServer社区中寻找解决方案,但找不到答案。
Exception in thread "main" com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.BadRequestException: Bad Request
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.DefaultErrorHandler.handleStatusCodeError(DefaultErrorHandler.java:111)
at com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.DefaultErrorHandler.handleError(DefaultErrorHandler.java:68)
at com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest.executeRequest(JerseyRequest.java:189)
at com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest.post(JerseyRequest.java:140)
at com.jaspersoft.jasperserver.jaxrs.client.apiadapters.resources.SingleResourceAdapter.uploadMultipartResource(SingleResourceAdapter.java:204)
at com.jaspersoft.jasperserver.jaxrs.client.apiadapters.resources.support.processor.CommonOperationProcessorImpl.create(CommonOperationProcessorImpl.java:46)
at com.jaspersoft.jasperserver.jaxrs.client.apiadapters.resources.support.decorator.ReportUnitResourceOperationProcessorDecorator.createInFolder(ReportUnitResourceOperationProcessorDecorator.java:46)
at com.jaspersoft.jasperserver.jaxrs.client.TestJasperDeploy.uploadingReportunit(TestJasperDeploy.java:170)
我初始化JasperServer的代码是
Properties props = new Properties() {
setProperty("url", "http://localhost:8080/jasperserver/");
setProperty("contentMimeType", "JSON");
setProperty("acceptMimeType", "JSON");
setProperty("connectionTimeout", "");
setProperty("readTimeout", "");
setProperty("authenticationType", "SPRING");
setProperty("restrictedHttpMethods", "false");
setProperty("logHttpEntity", "true");
setProperty("logHttp", "true");
setProperty("handleErrors", "true");
}
};
RestClientConfiguration configuration = RestClientConfiguration.loadConfiguration(props);
client = new JasperserverRestClient(configuration);
session = client.authenticate("jasperadmin", "jasperadmin");
我上传报告单位的代码是:
//this report has no subreports
// and requires a datasource not included in this example
ClientReferenceableFile jrxml = new ClientReferenceableFile() {
@ Override
public String getUri() {
//my local jrxm report
String fileString1 = "C:/reports/documentos/subreports/SR_D_01_nome_funcionario.jrxml";
return fileString1;
}
};
ClientReportUnit repunit = new ClientReportUnit();
repunit.setJrxml(jrxml);
//this is the report location on the server
repunit.setUri("/reports/documentos/subreports");
repunit.setLabel("label report unit");
repunit.setDescription("descricao report");
ClientFile clifile = new ClientFile();
clifile.setType(ClientFile.FileType.jrxml);
clifile.setUri("/reports/documentos/subreports");
ReportUnitResourceBuilder repUnut = session.resourcesService().resource(repunit).withJrxml(repunit.getUri(), clifile);
repUnut.createInFolder("/reports");
最佳答案
在这种情况下,您使用的API不正确。这是您应该拥有的:
ClientReportUnit reportUnit = new ClientReportUnit();
reportUnit.setLabel("label report unit");
reportUnit.setDescription("descricao report");
ClientFile clientFile = new ClientFile();
clientFile.setType(ClientFile.FileType.jrxml);
clientFile.setLabel("Main Report");
FileInputStream fis;
try {
fis = new FileInputStream("/path/to/report_template.jrxml");
ReportUnitResourceBuilder reportUnitResourceBuilder = session.resourcesService().resource(reportUnit).withJrxml(fis, clientFile);
reportUnitResourceBuilder.createInFolder("/reports/documentos/subreports");
} catch (FileNotFoundException e) {
// do something with the error
throw new RuntimeException(e);
}