如何在Jasper Server 6.3社区版通过REST 2 API设置报表数据适配器
Great question—this is a super common use case when you need to reuse the same report across multiple pre-configured data sources. Let’s walk through the step-by-step solution to make this work:
Step 1: Update Your JRXML for Dynamic Adapter Input
First, we’ll replace the hardcoded data adapter path in your report with a parameter that can be passed at runtime. This keeps your report flexible while maintaining backward compatibility.
- Add a string parameter to your JRXML (include a default value to fall back on if no parameter is provided):
<parameter name="DATA_ADAPTER_PATH" class="java.lang.String" defaultValue="common/test.xml"/>
- Modify the existing data adapter property to reference this parameter instead of the fixed path:
<property name="net.sf.jasperreports.data.adapter" value="$P{DATA_ADAPTER_PATH}"/>
Step 2: Execute the Report via REST v2 API with Your Target Adapter
Now you can call the JasperServer REST v2 API to run the report, specifying your desired data adapter path in the request body.
Example cURL Request
curl -X POST "http://your-jasper-server:8080/jasperserver/rest_v2/reports/[YOUR_REPORT_REPOSITORY_URI]/exports/pdf" \ -H "Content-Type: application/json" \ -H "Authorization: Basic [BASE64_ENCODED_CREDENTIALS]" \ -d '{ "parameters": { "DATA_ADAPTER_PATH": "common/another-db-adapter.xml" }, // Optional: Add export-specific settings like page ranges "pages": "1-5" }'
Important Details:
- Replace
[YOUR_REPORT_REPOSITORY_URI]with the full path to your report in JasperServer (e.g.,/reports/finance/monthly-forecast). - The
Authorizationheader uses Base64 encoding of your JasperServer username and password (e.g.,admin:adminbecomesYWRtaW46YWRtaW4=). - The
DATA_ADAPTER_PATHvalue must exactly match the path of the data adapter stored in your JasperServer repository—use the same path you see when navigating to the adapter in the web UI.
Step 3: Validate the Setup
To confirm everything works as expected:
- Check JasperServer’s logs for entries referencing the data adapter path you passed.
- Run the report with different adapter paths and verify the output matches the corresponding database’s data.
This approach works perfectly in JasperServer 6.3 Community Edition, letting you avoid maintaining duplicate report versions for different data sources.
内容的提问来源于stack exchange,提问作者Michael M

