帆软官方的数据工厂插件太贵了。自己也开发一个,先看下界面:
自己开发的现在支持HTTP,HTTPS的GET,POST请求,支持URL从配置文件读取,支持URL加密请求结果支持json,xml,支持对请求结果的扩展处理,支持对HTTP请求的BODY,HEADER参数扩展处理。
1 GET请求:
后台写法:
@CrossOrigin(origins = "*")
@RequestMapping(value = "/finereport", method = RequestMethod.GET)
@ResponseBody
public HttpReqResult finereport(HttpServletRequest req, HttpServletResponse res,
@RequestParam("name") String name, @RequestParam("age") int age) {
System.out.println("finereport");
ArrayList<HashMap<String, Object>> result = new ArrayList<>();
HashMap<String, Object> param = new HashMap<>();
result.add(param);
param.put("name", "aaass");
param.put("age", 1234);
param = new HashMap<>();
result.add(param);
param.put("name", "aas定时 方法");
param.put("age", 2356);
param = new HashMap<>();
result.add(param);
param.put("name", "322试试");
param.put("age", 111);
HashMap<String, Object> param1 = new HashMap<>();
param1.put("aaa", result);
return HttpReqResult.ok(param1);
}
返回结果为:
{"status":"ok","data":[{"name":"zhangsan","age":12},{"name":"试试 法","age":12},{"name":"是否 22","age":32}],"errorMsg":null}
界面配置:
点击预览可以看到返回的结果:
2 POST请求:
参数传递按json格式
后台写法:
@CrossOrigin(origins = "*")
@RequestMapping(value = "/finereport2", method = RequestMethod.POST)
@ResponseBody
public HttpReqResult finereport2(HttpServletRequest req, HttpServletResponse res,
@RequestBody FineReportBean fineReportBean) {
System.out.println("finereport2");
String token = req.getHeader("token");
System.out.println("token:"+token);
ArrayList<HashMap<String, Object>> result = new ArrayList<>();
HashMap<String, Object> param = new HashMap<>();
result.add(param);
param.put("name", "张三");
param.put("age", 34);
param = new HashMap<>();
result.add(param);
param.put("name", "李四");
param.put("age", 112);
param = new HashMap<>();
result.add(param);
param.put("name", "王五");
param.put("age", 1232);
return HttpReqResult.ok(result);
}
设计器配置:
预览结果:
参数传递按 application/x-www-form-urlencoded
后台代码:
@CrossOrigin(origins = "*")
@RequestMapping(value = "/finereport3", method = RequestMethod.POST)
@ResponseBody
public HttpReqResult finereport3(HttpServletRequest req, HttpServletResponse res,
@RequestParam("name") String name, @RequestParam("age") int age) {
System.out.println("finereport3:name="+name+",age="+age);
String token = req.getHeader("token");
System.out.println("token:"+token);
ArrayList<HashMap<String, Object>> result = new ArrayList<>();
HashMap<String, Object> param = new HashMap<>();
result.add(param);
param.put("name", "张三1");
param.put("age", 314);
param = new HashMap<>();
result.add(param);
param.put("name", "李四1");
param.put("age", 1121);
param = new HashMap<>();
result.add(param);
param.put("name", "王五1");
param.put("age", 12321);
param = new HashMap<>();
result.add(param);
param.put("name", "王五2");
param.put("age", 12321);
param = new HashMap<>();
result.add(param);
param.put("name", "王五3");
param.put("age", 12321);
param = new HashMap<>();
result.add(param);
param.put("name", "王五4");
param.put("age", 12321);
param = new HashMap<>();
result.add(param);
param.put("name", "王五5");
param.put("age", 12321);
return HttpReqResult.ok(result);
}
设计器配置:
预览结果:
参数传递按 multipart/form-data
后台代码:
//post 接收form-data参数
@CrossOrigin(origins = "*")
@RequestMapping(value = "/finereport4", method = RequestMethod.POST)
@ResponseBody
public HttpReqResult finereport4(HttpServletRequest req, HttpServletResponse res,
@RequestParam("name") String name, @RequestParam("age") int age
)
throws IOException {
System.out.println("finereport4:name="+name+",age="+age);
String token = req.getHeader("token");
System.out.println("token:"+token);
ArrayList<HashMap<String, Object>> result = new ArrayList<>();
HashMap<String, Object> param = new HashMap<>();
result.add(param);
param.put("name", "张三4");
param.put("age", 3144);
param = new HashMap<>();
result.add(param);
param.put("name", "李四4");
param.put("age", 11214);
param = new HashMap<>();
result.add(param);
param.put("name", "王五4");
param.put("age", 123214);
return HttpReqResult.ok(result);
}
设计器配置:
预览结果:
3 URL配置到文件中,并且URL可加密
使用场景是:
1 我们将URL全部写到配置文件中,设计器中只需要从下拉框选择。
2 如果我们有多套环境,每个环境的对应的URL不一致,但是我们再设计器中配好以后如果环境换了还需要去改模板里的URL。这个问题本插件也会解决。
3 如果不想别人看到我们的URL地址,本插件可以将URL加密,等真正运行时再去解密。
使用说明:
配置文件放到报表服务器的\webapps\webroot\dataset下,配置文件名字为:conf.xml
看下conf.xml的内容:
<root>
<httpUrls>
<httpUrl name="get请求" url="http://127.0.0.1:8082/finereport" publicKey=""></httpUrl>
<httpUrl name="post请求json" url="4c9USpp2zUsTI+0m0/rbw0NY52bJLxWQrYjcRsAyfo6mXQ5ZEYBb6E40mOZwn+5s" publicKey="ea26dtw34422**aa"></httpUrl>
<httpUrl name="post请求urlencoded" url="4c9USpp2zUsTI+0m0/rbw0NY52bJLxWQrYjcRsAyfo6X7f8iUoyP3E3iMD1KypmL" publicKey="ea26dtw34422**aa"></httpUrl>
<httpUrl name="post请求form-data" url="http://127.0.0.1:8082/finereport4" publicKey=""></httpUrl>
</httpUrls>
</root>
可以看到每个URL有三个属性:
name:url名字,显示再设计器中的下拉框中,模板中保存的也是这个名字。
url:实际的请求URL,可加密,
publicKey:解密密钥.
这些会出现再设计器中的下拉框中:
这样设计人员看不到具体的url,模板中也只保存了name属性,并且模板中保存的内容都是加密的。这样就把url地址保护起来了。
那URL如果加密呢?
再我提供的插件包中有一个plugin-data-factory-lib.jar,里面实现了一个加密解密的类。比如加密:
public static void main(String[] args) {
// TODO Auto-generated method stub
String publicKey = "ea26dtw34422**aa";
String src = "http://127.0.0.1:8082/finereport3";
try {
String dsc = SecretManager.Encrypt(src, publicKey);
System.out.println("dsc:"+dsc);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这样我们就可以得到加密后的url。