veryyoung的gravatar头像
veryyoung 2013-12-01 20:51:33

java webservice实例教程

最近在学习web services,网上先关的资料不多,讲的都是很基础,例子也很简单,自己动手敲了敲在这里和大家分享一下,希望能对初学者有所帮助。

 

Web Services服务器端开发

服务器端开发用的是XFire,版本是1.2.6,XFire现在已经成apache下面的一个项目CXF的一部分了,老早就不更新版本了,XFire过不过时我是不知道,不过还有一些人在用。

开发环境是:IDEA,Tomcat 8.0

新建一个项目,可以是web project也可以是web service project,区别不大。项目建好之后:(项目名假设为:webservice)

1、下载XFire1.2.6.jar

jar包下载地址 http://pan.baidu.com/s/1bZ8yf0

加压下载好的文件,将lib文件夹下所有jar包添加到项目中,并且将xfire-all-1.2.6.jar加入到项目中。

 

2、编写服务接口

public interface CalculatorService {
    public int add(int a,int b);
    public int sub(int a,int b);
    public int mul(int a,int b);
    public int div(int a,int b);
}

3、编写服务接口实现类

public class CalculatorServiceImpl implements CalculatorService{
 
    @Override
    public int add(int a, int b) {
        return  a+b;  //To change body of implemented methods use File | Settings | File Templates.
    }
 
    @Override
    public int sub(int a, int b) {
        return a-b;  //To change body of implemented methods use File | Settings | File Templates.
    }
 
    @Override
    public int mul(int a, int b) {
        return a*b;  //To change body of implemented methods use File | Settings | File Templates.
    }
 
    @Override
    public int div(int a, int b) {
        return a/b;  //To change body of implemented methods use File | Settings | File Templates.
    }
}

4、编写web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
 
    <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/services/XFireServlet/*</url-pattern>
    </servlet-mapping>
 
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
 
</web-app>

5、配置服务
在src目录下新建WEB-INF文件夹,在WEB-INF文件夹下新建xfire文件夹,在xfire下新建services.xml文件。

name表示服务的名字可以自己随便定义,serviceClass指明服务接口类,implementationClass指明服务实现类

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
    <service>
        <name>CalculatorService</name>
        <serviceClass>com.young.service.CalculatorService</serviceClass>
        <implementationClass>
            com.young.service.CalculatorServiceImpl
        </implementationClass>
 
    </service>
</beans>

6、启动服务

将该项目添加到tomcat中,启动tomcat,在浏览器中输入http://localhost:8080/webservice/services就能看到该项目下所有服务,点击服务后面的[wsdl],就会看到服务的wsdl文件内容。
java webservice实例教程

6、编写客户端
通过WSDL地址来创建动态客户端,代码如下

public class ClientTest {
    public static void main(String[] args) throws Exception {
        Client client = new Client(new URL("http://localhost:8080/webservice/services/CalculatorService?wsdl"));
        Object[] results = client.invoke("add", new Object[]{1, 2});
        System.out.println(results[0]);
    }
}

项目文件目录结构如下:
java webservice实例教程

7.跨语言编写客户端
前面编写的客户端采用的是java语言,与Service采用的是同一个JVM,无法直观的体会出webservice跨平台跨语言的特性
下面采用c#编写客户端
打开Visual Studio,新建一个c# console project,命名为wsclient,添加引用,选择添加web引用,输入http://localhost:8080/webservice/services/CalculatorService?wsdl
给该引用命名为CalculatorService
编写测试代码,代码如下


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using wsclient.CalculatorService;

namespace wsclient
{
    class Program
    {
        static void Main(string[] args)
        {
            CalculatorService.CalculatorService cal = new CalculatorService.CalculatorService();
            int result = cal.add(1, 2);
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}

点击运行,效果如下
java webservice实例教程

 

博文地址 http://veryyoung.sinaapp.com/?p=111


最代码官方编辑于2017-6-6 17:43:53


打赏

文件名:webservice.zip,文件大小:16.287K 下载
最代码最近下载分享源代码列表最近下载
Zeorwyc  LV8 2022年5月7日
dengjunjun  LV15 2022年3月28日
lironggang  LV38 2021年4月29日
心中无码  LV5 2020年8月17日
hehe264  LV20 2020年5月11日
羞羞小子  LV13 2020年4月17日
luqb890913  LV12 2019年7月24日
jeep123456  LV10 2019年7月13日
itcaizhe  LV9 2019年5月14日
wsupsup  LV16 2018年9月29日
最代码最近浏览分享源代码列表最近浏览
sunlea  LV18 5月8日
罗清晨  LV12 2月28日
lee123321  LV22 2023年12月19日
1529860026  LV24 2023年6月1日
漫步的海星  LV4 2023年3月14日
heqian  LV16 2023年1月10日
唯易人生  LV3 2022年10月28日
ewan007  LV29 2022年9月17日
ljt289917726  LV3 2022年9月5日
飞梦ff  LV8 2022年7月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友