欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

搭建webservice

程序员文章站 2022-06-08 14:31:20
...

(一)Grails下使用xfire搭建webservice


        Grails加上Xfire plugin 搭建web Service 环境,是非常简单的。 首先要给自己的Grails Application 应用工程装上Xfire 的插件.可以选择先下载zip包再安装也可以直由网络安装。

 

Type this command in your Grail application directory

//在你的应用程序目录下执行

$> grails install-plugin xfire

or if you have a plugin archive locally.

//如果插件已经下载到了本地,就用这个方法

$> grails install-plugin /path/to/grails-xfire-0.7.3.zip

 

 

          安装完成,grails控制台会有相应的提示。在grails的app-grailsPlugins会有xfire相应的jar包。

 

     

然后到 %GRAILS_APPLICATION%/conf目录下面去修改一下UrlMappings.groovy文件,记得一定要改.

 

 

 

 

 

 

static mappings = {

          "/$controller/$action?/$id?"{

              constraints {

                        controller(matches:/.*[^(services)].*/)

                  }

          }

}

//红色字体为新添加的部分

 

     接下来就是直接写属于你自己需要的业务服务类了,这里我假设我的服务类名为Test,CMD模式下输入:

 

   

create-service Test

 

   现在在grails-app的services下面有一个TestService.groovy,你可以拷贝下面的代码到你的TestService.groovy。

  

 

 

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
 

@WebService(name="Test",serviceName="Test",
  targetNamespace="http://www.oksonic.cn/xfire")
class TestService {
 

static expose=['xfire']
 

String helloXfire(@WebParam String name)
{
    return "Hello! ${name}";

}

 


 

   现在启动我们的项目,等待启动完成后,打开浏览器查看Test服务的wsdl,如过你能看到,就证明你已经成功一半了。

  

http://localhost:8080/%Grails_Application%/services/Test?wsdl

 

 

    下面我们可以进行测试了。在myeclipse下建立一个javaproject工程(需要导入下面的jar包)。建一个接口,接口的名字可以随便建,但最好还是和我们的webservice的名字相同,但是接口中的方法必须和webservice中的一样,这包括方面名,参数,返回类型。

  

接口 : 名称 TestService:

package TestService;

public interface SynAccountService123 {
 
 public String acceptDate(String name);

}

 

测试类:

 

package TestService;

import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

public class TestGrailsService {
 
 public static void main(String[] args) throws Exception {
  Service serviceModel = new ObjectServiceFactory().create( TestService.class);
  String r=null;
   TestServiceservice = (TestService) new XFireProxyFactory().create(serviceModel,
     "http://localhost:8080/APP/services/SynAccount");
   r=service.acceptDate("xfire");
   System.out.println(r);
 }
 

}

 

 

这样一个grails的webservice环境就搭好了。