`
leon1509
  • 浏览: 538185 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

groovy编写webservice服务端和客户端(含连接数据并输出JSON数据)

阅读更多
个人总结,网上几乎没有此方面资料,有一篇帖子,按照其内容无法运行。
注意:请以帖子内容为准,附件为参考

环境: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"
    }
]
  • ws.rar (919 Bytes)
  • 下载次数: 22
分享到:
评论

相关推荐

    Android JSON数据格式解析_服务端

    在Android开发中,JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,常用于客户端与服务端之间的数据传输。GSON是Google提供的一个Java库,它可以将Java对象转换为等效的JSON字符串,反之亦然,使得...

    mqtt协议实现 Android推送(服务端、客户端代码)

    本文将深入探讨如何使用MQTT协议在Android平台上实现推送服务,包括服务端和客户端的代码实现。 一、MQTT协议基础 1. **协议特点**:MQTT设计目标是低开销、低带宽和高可靠性,适用于移动设备和网络条件差的环境。...

    android客户端获取json

    在Android应用开发中,与服务器进行数据交互是常见的需求,其中JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,被广泛用于客户端与服务端之间的数据传输。本教程将聚焦于如何在Android客户端利用...

    Android_JSON数据解析

    在Android开发中,JSON(JavaScript Object Notation)是一种常用的数据交换格式,因其简洁、易读、易编写的特点,被广泛应用于服务器与客户端之间的数据传输。本篇将详细讲解如何在Android应用中解析JSON数据,主要...

    Groovy和Xquery脚本JSON和XML解析工具

    脚本解析工具,可使用Groovy或Xquery解析JSON报文或XML报文

    WebSocket安卓客户端实现详解(三)–服务端主动通知

    3. 服务端主动通知:WebSocket连接建立后,服务端可以通过已打开的连接向客户端发送数据。这通常通过在服务端代码中调用WebSocket对象的`send()`方法来实现。例如,如果服务端检测到新的数据或事件,它可以立即推送...

    groovy脚本实现对数据库的增删改查

    Groovy的动态特性使得代码更易于阅读和编写,而无需编译,这极大地提高了开发效率。 在实际项目中,Groovy还常与其他工具结合,如Grails(一个基于Groovy的Web框架),或者Gradle(构建自动化工具)。这些工具也...

    Android代码-使用 Groovy 语言编写gank.io客户端

    @Deprecated Official Android Support Library 23.2 added support for Vector Drawables and Animated Vector Drawables. Please, use that. This lib is still usable, but it is no longer maintained. ...

    Android 实用的数据json数据解析封装类

    在Android开发中,JSON(JavaScript Object Notation)是一种常用的数据交换格式,因其轻量级、易读易写的特点,被广泛应用于服务器与客户端之间的数据传输。对于Android开发者来说,理解和熟练掌握JSON解析至关重要...

    JSON入门Java篇-3-用json.org来构建JSON数据并输出.rar

    本教程将详细介绍如何使用json.org的Java API来构建和输出JSON数据。 首先,你需要在项目中引入json.org的库。可以通过Maven或Gradle来添加依赖,如果是Maven,可以在pom.xml文件中添加以下依赖: ```xml ...

    android json解析(包含服务器端,客户端)

    总结,理解并熟练掌握JSON解析是Android开发者必备的技能之一,无论是在客户端解析服务器返回的数据,还是在服务器端处理客户端的请求,JSON都起着至关重要的作用。通过选择合适的库和遵循最佳实践,可以使你的JSON...

    groovy框架数据收集

    Groovy框架在数据收集中的应用主要涉及到数据的获取、处理和存储,这通常是一个复杂而关键的任务。Groovy,作为一种动态、简洁的编程语言,它能够无缝集成Java平台,因此在构建数据采集系统时,Groovy可以提供高效且...

    Android下调用webservice的客户端和服务器端详细代码

    在Android开发中,调用Web Service是常见的数据...在Android_webservice_demo压缩包中,包含了完整的Android客户端和服务器端代码,可以下载解压后运行,以帮助开发者更好地理解和实践Android调用Web Service的过程。

    Android向Java后台发送请求并返回Json数据

    本教程将详细讲解如何从Android客户端向Java后台发送请求,并接收返回的Json数据。 一、HTTP请求库的选择 在Android中,我们可以使用多种库来实现网络请求,如HttpURLConnection(原生API)、Volley、Retrofit、...

    Android端解析web端传来的json数据

    JSON因其轻量级、易于阅读和编写的特点,被广泛用于服务器向客户端发送结构化数据。本篇文章将深入探讨如何在Android端解析Web端传来的JSON数据。 首先,了解JSON的基本结构至关重要。JSON是一种数据交换格式,它...

    groovy

    3. 数据处理:Groovy可以方便地处理XML、JSON等数据格式,常用于数据转换和解析。 4. 测试:Spock测试框架使用Groovy编写,提供了富有表达力的测试语句,简化了测试代码的编写。 5. 容器管理:在Apache Kafka、...

    Android Studio下使用Google Gson解析Json数据示例

    在Android开发中,数据交换和存储经常涉及到JSON格式,因为它是一种轻量级、易于阅读和编写的文本格式。Google Gson库是Java平台上的一个强大工具,用于将Java对象转换为对应的JSON表示,反之亦然。在Android Studio...

    androi中json解析数据

    JSON是一种轻量级的数据表示格式,它基于JavaScript的一个子集,易于人阅读和编写,同时也易于机器解析和生成。JSON数据通常包含键值对(key-value pairs)和数组。例如: ```json { "name": "John", "age": 30, ...

    groovy-all

    7. **Groovy JSON Support**: 内置的JSON处理能力,使得Groovy可以方便地解析和生成JSON格式的数据。 8. **Groovy HTML and XML Processing**: 提供了简洁的API来处理HTML和XML文档,如GPath和XmlSlurper。 9. **...

Global site tag (gtag.js) - Google Analytics