个人总结,网上几乎没有此方面资料,有一篇帖子,按照其内容无法运行。
注意:请以帖子内容为准,附件为参考
环境:windows7+jdk1.6.0.31+groovy1.8.6
1. 安装jdk和groovy并配置环境变量,略。
2. 安装groovyws0.5.2
命令行>:grape install org.codehaus.groovy.modules groovyws 0.5.2
3. 编写服务(这里写2个例子,一个是算术、一个是格式化日期):
1)算术服务
class MathService { double add(double arg0, double arg1) { return (arg0 + arg1) } double square(double arg0) { return (arg0 * arg0) } }
2)格式化日期服务
class DateFormatService{ String format(long time){ new Date(time).format('yyyy年MM月dd日') } }
3. 编写服务启动groovy脚本
import groovyx.net.ws.WSServer @Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2') def server = new WSServer() server.setNode("DateFormatService", "http://10.14.3.149:6980/DateFormatService") server.start() def s1 = new WSServer() s1.setNode("MathService", "http://10.14.3.149:6980/MathService") s1.start()
4. 编写客户端测试groovy脚本
import groovyx.net.ws.WSClient @Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2') def printDate(){ def proxy = new WSClient("http://10.14.3.149:6980/DateFormatService?wsdl", this.class.classLoader) proxy.initialize() println "今天是${proxy.format(new Date().time)}" } printDate()
============= 华丽的分割线 =================
补充:编写连接数据库的服务并返回JSON格式数据
DataService.groovy
import groovy.sql.Sql import groovy.json.JsonOutput class DataService{ String poses(){ def db = Sql.newInstance("jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.14.3.148)(PORT = 1521)))(CONNECT_DATA =(SID = orcl)))","ecard","ecard","oracle.jdbc.driver.OracleDriver") def poses = db.rows("select * from OCS_DM_Terminal ") def retJson = new JsonOutput() return retJson.toJson(poses) } }
并添加到服务启动的groovy脚本中:
import groovyx.net.ws.WSServer @Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2') def server = new WSServer() server.setNode("DateFormatService", "http://10.14.131.114:6980/DateFormatService") server.start() def s1 = new WSServer() s1.setNode("MathService", "http://10.14.131.114:6980/MathService") s1.start() def s2 = new WSServer() s2.setNode("DataService", "http://10.14.131.114:6980/DataService") s2.start()
调用方法1:
import groovyx.net.ws.WSClient @Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2') def test(){ def url = "http://10.14.131.114:6980/DataService?WSDL" def proxy = new WSClient(url, this.class.classLoader) proxy.initialize() def ret = proxy.poses() println ret.toString() } test()
输出结果:
[{"ID":181,"SYS_CODE":"02","CLIENT_ID":null,"GROUP_ID":24,"MERC_NO":41,"TYPE":null,"KIND":6,"STATIONNO":2,"NAME":null,"STATIONNO_ID":null,"DESCRIPTION": null,"BANK_ID":"10019999","SET_DATE":"2011-10-08T08:32:29+0000","FLAG":"0","BLACK_FLAG":0,"CANCEL_BLACK_FLAG":0,"BLACK_LIST_ID":0,"TERM_NAME":null,"MENU _VERSION":"0","BATCHNO":"000000"},{"ID":161,"SYS_CODE":"04","CLIENT_ID":null,"GROUP_ID":205,"MERC_NO":1,"TYPE":null,"KIND":6,"STATIONNO":4,"NAME":null," STATIONNO_ID":null,"DESCRIPTION":null,"BANK_ID":"55555555","SET_DATE":"2011-09-17T03:38:33+0000","FLAG":"1","BLACK_FLAG":0,"CANCEL_BLACK_FLAG":0,"BLACK_ LIST_ID":0,"TERM_NAME":null,"MENU_VERSION":"0","BATCHNO":"000000"},{"ID":101,"SYS_CODE":"01","CLIENT_ID":205,"GROUP_ID":124,"MERC_NO":1,"TYPE":"11","KIN D":7,"STATIONNO":1,"NAME":null,"STATIONNO_ID":null,"DESCRIPTION":null,"BANK_ID":"22019999","SET_DATE":"2011-09-26T08:40:24+0000","FLAG":"0","BLACK_FLAG" :0,"CANCEL_BLACK_FLAG":0,"BLACK_LIST_ID":0,"TERM_NAME":null,"MENU_VERSION":"0520110916170215","BATCHNO":"000012"},{"ID":102,"SYS_CODE":"02","CLIENT_ID": 26,"GROUP_ID":24,"MERC_NO":41,"TYPE":"11","KIND":8,"STATIONNO":1,"NAME":null,"STATIONNO_ID":null,"DESCRIPTION":null,"BANK_ID":"11019999","SET_DATE":"201 1-09-16T05:37:24+0000","FLAG":"1","BLACK_FLAG":0,"CANCEL_BLACK_FLAG":0,"BLACK_LIST_ID":0,"TERM_NAME":null,"MENU_VERSION":"0520110906111247","BATCHNO":"0 00000"},{"ID":121,"SYS_CODE":"01","CLIENT_ID":null,"GROUP_ID":124,"MERC_NO":1,"TYPE":null,"KIND":6,"STATIONNO":2,"NAME":null,"STATIONNO_ID":null,"DESCRI PTION":null,"BANK_ID":"33019999","SET_DATE":"2011-10-08T01:34:28+0000","FLAG":"0","BLACK_FLAG":0,"CANCEL_BLACK_FLAG":0,"BLACK_LIST_ID":0,"TERM_NAME":nul l,"MENU_VERSION":"0520110916170215","BATCHNO":"000000"},{"ID":141,"SYS_CODE":"01","CLIENT_ID":null,"GROUP_ID":124,"MERC_NO":41,"TYPE":null,"KIND":9,"STA TIONNO":3,"NAME":null,"STATIONNO_ID":null,"DESCRIPTION":null,"BANK_ID":"33333333","SET_DATE":"2011-10-10T08:19:57+0000","FLAG":"0","BLACK_FLAG":0,"CANCE L_BLACK_FLAG":0,"BLACK_LIST_ID":0,"TERM_NAME":null,"MENU_VERSION":"0","BATCHNO":"000000"},{"ID":201,"SYS_CODE":"02","CLIENT_ID":325,"GROUP_ID":24,"MERC_ NO":41,"TYPE":"11","KIND":8,"STATIONNO":3,"NAME":"\u89E6\u6478\u5C4F","STATIONNO_ID":"3","DESCRIPTION":null,"BANK_ID":"10010001","SET_DATE":"2011-10-09T 09:35:50+0000","FLAG":"0","BLACK_FLAG":0,"CANCEL_BLACK_FLAG":0,"BLACK_LIST_ID":0,"TERM_NAME":null,"MENU_VERSION":"0","BATCHNO":"000000"}]
调用方法2:
import groovyx.net.ws.WSClient import groovy.json.JsonSlurper @Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2') def test(){ def url = "http://10.14.131.114:6980/DataService?WSDL" def proxy = new WSClient(url, this.class.classLoader) proxy.initialize() def ret = proxy.poses() def slurper = new JsonSlurper() def states = slurper.parseText(ret) states.each{ println it.NAME } // println ret } test()
输出结果:
null null null null null null 触摸屏
调用方法3:
import groovyx.net.ws.WSClient import groovy.json.JsonSlurper import groovy.json.JsonOutput import groovy.json.JsonBuilder @Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2') def test(){ def url = "http://10.14.131.114:6980/DataService?WSDL" def proxy = new WSClient(url, this.class.classLoader) proxy.initialize() def ret = proxy.poses() def slurper = new JsonSlurper() def states = slurper.parseText(ret) def x = new JsonBuilder (states) println x.toPrettyString() //println JsonOutput.prettyPrint(states.toString()) /* states.each{ println it.NAME }*/ // println ret } test()
输出结果:
[ { "GROUP_ID": 24, "TERM_NAME": null, "NAME": null, "DESCRIPTION": null, "CLIENT_ID": null, "STATIONNO": 2, "MENU_VERSION": "0", "BLACK_FLAG": 0, "MERC_NO": 41, "BATCHNO": "000000", "SET_DATE": "2011-10-08T08:32:29+0000", "ID": 181, "SYS_CODE": "02", "BANK_ID": "10019999", "BLACK_LIST_ID": 0, "STATIONNO_ID": null, "KIND": 6, "CANCEL_BLACK_FLAG": 0, "FLAG": "0", "TYPE": null }, { "GROUP_ID": 205, "TERM_NAME": null, "NAME": null, "DESCRIPTION": null, "CLIENT_ID": null, "STATIONNO": 4, "MENU_VERSION": "0", "BLACK_FLAG": 0, "MERC_NO": 1, "BATCHNO": "000000", "SET_DATE": "2011-09-17T03:38:33+0000", "ID": 161, "SYS_CODE": "04", "BANK_ID": "55555555", "BLACK_LIST_ID": 0, "STATIONNO_ID": null, "KIND": 6, "CANCEL_BLACK_FLAG": 0, "FLAG": "1", "TYPE": null }, { "GROUP_ID": 124, "TERM_NAME": null, "NAME": null, "DESCRIPTION": null, "CLIENT_ID": 205, "STATIONNO": 1, "MENU_VERSION": "0520110916170215", "BLACK_FLAG": 0, "MERC_NO": 1, "BATCHNO": "000012", "SET_DATE": "2011-09-26T08:40:24+0000", "ID": 101, "SYS_CODE": "01", "BANK_ID": "22019999", "BLACK_LIST_ID": 0, "STATIONNO_ID": null, "KIND": 7, "CANCEL_BLACK_FLAG": 0, "FLAG": "0", "TYPE": "11" }, { "GROUP_ID": 24, "TERM_NAME": null, "NAME": null, "DESCRIPTION": null, "CLIENT_ID": 26, "STATIONNO": 1, "MENU_VERSION": "0520110906111247", "BLACK_FLAG": 0, "MERC_NO": 41, "BATCHNO": "000000", "SET_DATE": "2011-09-16T05:37:24+0000", "ID": 102, "SYS_CODE": "02", "BANK_ID": "11019999", "BLACK_LIST_ID": 0, "STATIONNO_ID": null, "KIND": 8, "CANCEL_BLACK_FLAG": 0, "FLAG": "1", "TYPE": "11" }, { "GROUP_ID": 124, "TERM_NAME": null, "NAME": null, "DESCRIPTION": null, "CLIENT_ID": null, "STATIONNO": 2, "MENU_VERSION": "0520110916170215", "BLACK_FLAG": 0, "MERC_NO": 1, "BATCHNO": "000000", "SET_DATE": "2011-10-08T01:34:28+0000", "ID": 121, "SYS_CODE": "01", "BANK_ID": "33019999", "BLACK_LIST_ID": 0, "STATIONNO_ID": null, "KIND": 6, "CANCEL_BLACK_FLAG": 0, "FLAG": "0", "TYPE": null }, { "GROUP_ID": 124, "TERM_NAME": null, "NAME": null, "DESCRIPTION": null, "CLIENT_ID": null, "STATIONNO": 3, "MENU_VERSION": "0", "BLACK_FLAG": 0, "MERC_NO": 41, "BATCHNO": "000000", "SET_DATE": "2011-10-10T08:19:57+0000", "ID": 141, "SYS_CODE": "01", "BANK_ID": "33333333", "BLACK_LIST_ID": 0, "STATIONNO_ID": null, "KIND": 9, "CANCEL_BLACK_FLAG": 0, "FLAG": "0", "TYPE": null }, { "GROUP_ID": 24, "TERM_NAME": null, "NAME": "触摸屏", "DESCRIPTION": null, "CLIENT_ID": 325, "STATIONNO": 3, "MENU_VERSION": "0", "BLACK_FLAG": 0, "MERC_NO": 41, "BATCHNO": "000000", "SET_DATE": "2011-10-09T09:35:50+0000", "ID": 201, "SYS_CODE": "02", "BANK_ID": "10010001", "BLACK_LIST_ID": 0, "STATIONNO_ID": "3", "KIND": 8, "CANCEL_BLACK_FLAG": 0, "FLAG": "0", "TYPE": "11" } ]