groovy编写webservice

使用java编写webservice已经简单多了,但是要借助第三方工具或者框架,比如spring,学习成本还是比较高的。

groovy可以瞬间搭建起webservice的服务。

webservice基本原理

先简单介绍一下webservice。一般使用的是http协议(也可以是其他传输协议的),传输xml格式文本信息。一般用webservice做RPC,也就是Remote Procedure Call,即远程过程调用。

远程过程调用是和本地过程调用相对的,本地过程调用,就是比如java代码里面调用一个方法,那么有方法参数,调用后有返回值。

远程过程调用无非是通过网络远程调用一个“方法”而已。可以近似的认为,方法名是一个url,方法的参数是http请求部分,返回值是http响应。

webservice通过http传输的文本格式,是遵循xml格式的,而且,可以看出有一定的文档格式,这个格式是就是soap协议。

刚才说的都是比较底层和基本的原理,实际上编程语言调用webservice,使用编程语言自带的webservice API,已经不会碰到这样底层的东西了,比如说操作http请求和响应,比如java,看上去很像调用一个java方法一样。


groovy编写webservice的准备工作

groovy核心部分并不支持webservice,需要安装groovy模块,因为是groovy 1.6,可以使用方便的grape自动解决类库依赖。以下环境均为在ubuntu下情况,windows下类似。

grape如想正确得到modules(模块),需要配置用户目录下~/.groovy/grapeConfig.xml,具体内容,可参见:

http://groovy.codehaus.org/GroovyWS+grape+config+file

这里也贴出来:

<ivysettings>
  <settings defaultResolver="downloadGrapes"/>
  <resolvers>
    <chain name="downloadGrapes">
      <filesystem name="cachedGrapes">
        <ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/>
        <artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]"/>
      </filesystem>
      <!-- todo add 'endorsed groovy extensions' resolver here -->
      <ibiblio name="codehaus" root="http://repository.codehaus.org/" m2compatible="true"/>
      <ibiblio name="snapshots.codehaus" root="http://snapshots.repository.codehaus.org/" m2compatible="true"/>
      <ibiblio name="apache" root="http://people.apache.org/repo/m2-ibiblio-rsync-repository/" m2compatible="true"/>
      <ibiblio name="apache-incubating" root="http://people.apache.org/repo/m2-incubating-repository/" m2compatible="true"/>
      <ibiblio name="maven" root="http://repo2.maven.org/maven2/" m2compatible="true"/>
    </chain>
  </resolvers>
</ivysettings>

 

然后,命令行下执行:

grape install org.codehaus.groovy.modules groovyws 0.5.1

需要漫长的等待,因为要下载无数包。

groovy编写webservice服务端

webservice服务端,就是RPC要调用的那个过程,比如一个方法:

public String getFormatDate(long time)

传一个毫秒数,返回中文的年月日字符串。看看groovy怎么写。

groovy代码:

#!/usr/bin/env groovy

import groovyx.net.ws.*

@Grab(group=’org.codehaus.groovy.modules’, module=’groovyws’, version=’0.5.1′)

class DateFormatService{

    String format(long time){

        new Date(time).format(‘yyyy年MM月dd日’)

    }

}

def server=new WSServer()

server.setNode("DateFormatService", "http://localhost:9090/DateFormatService")

server.start()

 

在命令行执行这个代码,可以看到类似如下的信息。

image

说明webservice服务跑起来了。

groovy编写调用webservice的客户端

客户端只需知道webservice服务端的url即可,这里是:

http://localhost:9090/DateFormatService?wsdl

可以通过浏览器访问该链接,wsdl是webservices的描述语言(Web Services Description Language)。用于描述该服务需要传什么格式参数,返回值是什么格式等等。

客户端groovy程序API会根据这个wsdl文件,自动生成一个本地的代理类,就和远端的那个DateFormatService一样,这样调用就像本地调用似的。

代码:

#!/usr/bin/env groovy

import groovyx.net.ws.*

@Grab(group=’org.codehaus.groovy.modules’, module=’groovyws’, version=’0.5.1′)

def printDate(){

    def proxy = new WSClient("http://localhost:9090/DateFormatService?wsdl", this.class.classLoader)

    proxy.initialize()

    println "今天是${proxy.format(new Date().time)}"

}

printDate()

 

调用结果:

image

本文资源

groovyws官方网址:

http://groovy.codehaus.org/GroovyWS

本文源代码:

http://easymorse.googlecode.com/svn/trunk/groovy_ws/

PDF格式打印機    发送文章为PDF   

这篇文章上的评论的 RSS feed TrackBack URI

Leave a Reply