Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。

Web Service的关键技术和规则:

1.XML:描述数据的标准方法.

2.SOAP:表示信息交换的协议(简单对象访问协议).

3.WSDL:Web服务描述语言.

4.UDDI:通用描述、发现与集成,他是一种独立于平台,基于XML语言的用于在互联网上描述商务的协议。

一、利用JDK web服务api实现,这里使用基于SOAP message的Web Service:

1.首先创建一个Web Services项目,作为Web services Endpoint.

2.创建一个HelloService.java类

  1. package com.yjpeng.hello;
  2. import javax.jws.WebService;
  3. import javax.jws.WebMethod;
  4. import javax.xml.ws.Endpoint;
  5. @WebService
  6. public class HelloService {
  7. @WebMethod
  8. public String sayHello(String message){
  9. return "Hello ," + message;
  10. }
  11. public static void main(String[] args) {
  12. //create and publish an endPoint
  13. HelloService hello = new HelloService();
  14. Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);
  15. }
  16. }
  1. package com.yjpeng.hello;
  2. import javax.jws.WebService;
  3. import javax.jws.WebMethod;
  4. import javax.xml.ws.Endpoint;
  5. @WebService
  6. public class HelloService {
  7. @WebMethod
  8. public String sayHello(String message){
  9. return "Hello ," + message;
  10. }
  11. public static void main(String[] args) {
  12. //create and publish an endPoint
  13. HelloService hello = new HelloService();
  14. Endpoint endPoint = Endpoint.publish("http://localhost:8080/helloService", hello);
  15. }
  16. }

3.使用apt编译HelloService.java(例如: apt -d bin(bin存放编译后的文件目录) scr/com/yjpeng/hello/HelloService.java)后,会生成jaxws目录。

4.使用java com.yjpeng.hello.HelloService执行HelloService.java文件,在浏览器中输入http://localhost:8080/helloService?wsdl出现如下图

5.使用wsimport命令生成客户端:wsimport -p com.yjpeng.webservice -keep http://localhost:8080/helloService?wsdl 这时会在当前目录中生成如下文件:

6.编写好客户端文件HelloClient.java

  1. package com.yjpeng.hello;
  2. import com.yjpeng.webservice.HelloServiceService;
  3. public class HelloClient {
  4. public static
    void main(String[] args) {
  5. HelloServiceService helloServiceService =
    new HelloServiceService();
  6. com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort();
  7. System.out.println(helloService.sayHello("你好"));
  8. }
  9. }
  1. package com.yjpeng.hello;
  2. import com.yjpeng.webservice.HelloServiceService;
  3. public class HelloClient {
  4. public static void main(String[] args) {
  5. HelloServiceService helloServiceService = new HelloServiceService();
  6. com.yjpeng.webservice.HelloService helloService = helloServiceService.getHelloServicePort();
  7. System.out.println(helloService.sayHello("你好"));
  8. }
  9. }

运行结果在控制台输出 hello,你好  证明利用JDK web服务API实现web service成功!

二、使用xfire框架,我这里使用的是MyEclipse集成的xfire进行测试,利用xfire开发WebService可以有三种方法:

a.一种是从JavaBean中生成。

b.一种是从wsdl文件中生成。

c.一种是自己建立webservice。

具体实现步骤如下:

1.用Myeclipse建立webService工程(注意:Web Service&J2EE Details中的Framework选XFire),目录结构如下:

2.创建IHello.java接口

  1. package com.yjpeng.hello;
  2. public interface IHello {
  3. public String sayHello(String message);
  4. }
  1. package com.yjpeng.hello;
  2. public interface IHello {
  3. public String sayHello(String message);
  4. }

3.创建IHelloImpl.java实现IHello.java接口

  1. package com.yjpeng.hello;
  2. public class IHelloImpl implements IHello {
  3. public String sayHello(String message) {
  4. return message;
  5. }
  6. }
  1. package com.yjpeng.hello;
  2. public class IHelloImpl implements IHello {
  3. public String sayHello(String message) {
  4. return message;
  5. }
  6. }

4.修改Service.xml文件,加入以下代码

<service>
<!-- 为该Service起一个名字 -->
  <name>HelloService</name>
  <!-- service的接口类 -->
  <serviceClass>
  com.yjpeng.hello.IHello
  </serviceClass>
  <!-- service的接口实现类-->
  <implementationClass>
  com.yjpeng.hello.IHelloImpl
  </implementationClass>
  <!-- wsdl的样式-->
  <style>wrapped</style>
  <use>literal</use>
  <scope>application</scope>
</service>

5.把该Web Service项目部署到tomcat里面启动tomcat在浏览器地址栏输入http://localhost:8080/TestWebServices/services/Hello?wsdl 出现如下图

6.然后在展开HelloService后面的wsdl可以看到

7.创建一个客户端HelloClient.java类

  1. import java.net.MalformedURLException;
  2. import java.net.URL;
  3. import org.codehaus.xfire.XFireFactory;
  4. import org.codehaus.xfire.client.Client;
  5. import org.codehaus.xfire.client.XFireProxyFactory;
  6. import org.codehaus.xfire.service.Service;
  7. import org.codehaus.xfire.service.binding.ObjectServiceFactory;
  8. import com.yjpeng.hello.IHello;
  9. public class HelloClient {
  10. public static
    void main(String[] args) {
  11. Service s = new ObjectServiceFactory().create(IHello.class);
  12. XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
  13. String url="http://192.168.122.128:8080/TestWebServices/services/HelloService";
  14. IHello hello;
  15. try {
  16. hello = (IHello)xf.create(s, url);
  17. System.out.println(hello.sayHello("你好"));
  18. } catch (MalformedURLException e) {
  19. e.printStackTrace();
  20. }
  21. try {
  22. //这个是在java端调用.net写的远程Web Service 如果调用本机写的只需要把URL中的地址换成本机能访问的地址即可
  23. Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
  24. Object[] o = c.invoke("qqCheckOnline",
    new String[]{"271751507"});
  25. System.out.println(o.length);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  1. import java.net.MalformedURLException;
  2. import java.net.URL;
  3. import org.codehaus.xfire.XFireFactory;
  4. import org.codehaus.xfire.client.Client;
  5. import org.codehaus.xfire.client.XFireProxyFactory;
  6. import org.codehaus.xfire.service.Service;
  7. import org.codehaus.xfire.service.binding.ObjectServiceFactory;
  8. import com.yjpeng.hello.IHello;
  9. public class HelloClient {
  10. public static void main(String[] args) {
  11. Service s = new ObjectServiceFactory().create(IHello.class);
  12. XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
  13. String url="http://192.168.122.128:8080/TestWebServices/services/HelloService";
  14. IHello hello;
  15. try {
  16. hello = (IHello)xf.create(s, url);
  17. System.out.println(hello.sayHello("你好"));
  18. } catch (MalformedURLException e) {
  19. e.printStackTrace();
  20. }
  21. try {
  22. //这个是在java端调用.net写的远程Web Service 如果调用本机写的只需要把URL中的地址换成本机能访问的地址即可
  23. Client c = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
  24. Object[] o = c.invoke("qqCheckOnline", new String[]{"271751507"});
  25. System.out.println(o.length);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }

运行HelloClient.java类可以输出 你好            1.

三、使用axis1.4开发webservice方法

首先下载axis1.4包和tomcat服务器,并将解压后的axis1.4包下面的webapps下的axis目录复制到tomcat服务器的webapps文件夹中。axis支持三种Web Service的部署和开发,分别为:

a.Dynamic Invocation Interface(DII)

b.Stubs 方式

c.Dynamic Proxy方式

1.编写DII(Dynamic Invocation Interface)方式Web Service

a.编写服务程序HelloClient.java

  1. public class HelloClient
  2. {
  3. public String getName(String name){
  4. return "hello," + name;
  5. }
  6. }
  1. public class HelloClient
  2. {
  3. public String getName(String name){
  4. return "hello," + name;
  5. }
  6. }

b.将HelloClient.java文件拷贝到axis_home下,重命名为HelloClient.jws.

c.访问链接http://localhost:8080/axis/HelloClient.jws?wsdl页面显示axis自动生成的wsdl文件

d.编写访问服务的客户端TestHelloClient.java需要导入相应的axis.jar包,在下载的axis的WEB-INF/lib/目录下。

  1. package com.yjpeng.webservice;
  2. import java.net.URL;
  3. import javax.xml.namespace.QName;
  4. import org.apache.axis.client.Call;
  5. import org.apache.axis.client.Service;
  6. public class TestHelloClient {
  7. public static
    void main(String[] args){
  8. try{
  9. String endpoint = "http://localhost:8080/axis/HelloClient.jws";
  10. Service service = new Service();
  11. Call call = (Call)service.createCall();
  12. call.setOperationName(new QName(endpoint,
    "getName"));
  13. call.setTargetEndpointAddress(new URL(endpoint));
  14. String result = (String) call.invoke(new Object[]{"张三"});
  15. System.out.println(result);
  16. }catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  1. package com.yjpeng.webservice;
  2. import java.net.URL;
  3. import javax.xml.namespace.QName;
  4. import org.apache.axis.client.Call;
  5. import org.apache.axis.client.Service;
  6. public class TestHelloClient {
  7. public static void main(String[] args){
  8. try{
  9. String endpoint = "http://localhost:8080/axis/HelloClient.jws";
  10. Service service = new Service();
  11. Call call = (Call)service.createCall();
  12. call.setOperationName(new QName(endpoint, "getName"));
  13. call.setTargetEndpointAddress(new URL(endpoint));
  14. String result = (String) call.invoke(new Object[]{"张三"});
  15. System.out.println(result);
  16. }catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }

运行TestHelloClient.java在控制台输出hell,张三,测试成功.

2.编写Dynamci Proxy方式访问服务

a.编写部署服务端程序,用上边DII方式使用的HelloClient.java

  1. public class HelloClient
  2. {
  3. public String getName(String name){
  4. return "hello," + name;
  5. }
  6. }
  1. public class HelloClient
  2. {
  3. public String getName(String name){
  4. return "hello," + name;
  5. }
  6. }

b.编写代理接口HelloClientInterface.java需要扩展java.rmi.Remote类

  1. package com.yjpeng.dynamic.proxy;
  2. import java.rmi.Remote;
  3. import java.rmi.RemoteException;
  4. public interface HelloClientInterface extends Remote {
  5. public String getName(String name) throws RemoteException;
  6. }
  1. package com.yjpeng.dynamic.proxy;
  2. import java.rmi.Remote;
  3. import java.rmi.RemoteException;
  4. public interface HelloClientInterface extends Remote {
  5. public String getName(String name) throws RemoteException;
  6. }

c.编写访问服务的客户端TestHelloClient.java

  1. package com.yjpeng.dynamic.proxy;
  2. import java.net.URL;
  3. import javax.xml.namespace.QName;
  4. import javax.xml.rpc.Service;
  5. import javax.xml.rpc.ServiceFactory;
  6. public class TestHelloClient {
  7. public static
    void main(String[] args){
  8. try{
  9. String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl";
  10. String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws";
  11. String serviceName = "HelloClientService";
  12. String portName = "HelloClient";
  13. ServiceFactory serviceFactory = ServiceFactory.newInstance();
  14. Service afService = serviceFactory.createService(new URL(wsdlUrl),
    new QName(nameSpaceUrl, serviceName));
  15. HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),
  16. HelloClientInterface.class);
  17. System.out.println(proxy.getName("张三"));
  18. }catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }
  1. package com.yjpeng.dynamic.proxy;
  2. import java.net.URL;
  3. import javax.xml.namespace.QName;
  4. import javax.xml.rpc.Service;
  5. import javax.xml.rpc.ServiceFactory;
  6. public class TestHelloClient {
  7. public static void main(String[] args){
  8. try{
  9. String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl";
  10. String nameSpaceUrl = "http://localhost:8080/axis/HelloClient.jws";
  11. String serviceName = "HelloClientService";
  12. String portName = "HelloClient";
  13. ServiceFactory serviceFactory = ServiceFactory.newInstance();
  14. Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUrl, serviceName));
  15. HelloClientInterface proxy = (HelloClientInterface)afService.getPort(new QName(nameSpaceUrl, portName),
  16. HelloClientInterface.class);
  17. System.out.println(proxy.getName("张三"));
  18. }catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }

运行TestHelloClient.java在控制台输出hell,张三,测试成功.

四、使用axis2开发webservice

研究中....

五、在java web项目中开放一个webservice接口实例

1.引入需要的jar包

2.创建一个接口类IAddNumbers.java

  1. package com.yjpeng.webservice;
  2. public interface IAddNumbers {
  3. public int addNumbers(int a,
    int b);
  4. }
  1. package com.yjpeng.webservice;
  2. public interface IAddNumbers {
  3. public int addNumbers(int a, int b);
  4. }

3.创建一个AddNumberImpl.java实现IAddnumbers.java接口类

  1. package com.yjpeng.webservice;
  2. import javax.jws.WebService;
  3. import com.sun.xml.ws.transport.http.servlet.WSServlet;
  4. @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService",
  5. portName="AddNumberImpl")
  6. public class AddNumberImpl implements IAddNumbers {
  7. public int addNumbers(int a,
    int b) {
  8. return a + b;
  9. }
  10. }
  1. package com.yjpeng.webservice;
  2. import javax.jws.WebService;
  3. import com.sun.xml.ws.transport.http.servlet.WSServlet;
  4. @WebService(targetNamespace="http://webservice.yjpeng.com", serviceName="AddNumberImplService",
  5. portName="AddNumberImpl")
  6. public class AddNumberImpl implements IAddNumbers {
  7. public int addNumbers(int a, int b) {
  8. return a + b;
  9. }
  10. }

4.在WEN-INF目录下创建一个sun-jaxws.xml文件

  1. <?xml version="1.0"?>
  2. <endpoints version="2.0"
  3. xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
  4. <endpoint name="AddNumberImpl"
  5. implementation="com.yjpeng.webservice.AddNumberImpl"
  6. url-pattern="/addNumberImpl"/>
  7. </endpoints>
  1. <?xml version="1.0"?>
  2. <endpoints version="2.0"
  3. xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
  4. <endpoint name="AddNumberImpl"
  5. implementation="com.yjpeng.webservice.AddNumberImpl"
  6. url-pattern="/addNumberImpl"/>
  7. </endpoints>

5.在web.xml文件中增加

  1. <servlet>
  2. <servlet-name>AddNumberService</servlet-name>
  3. <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
  4. <load-on-startup>1</load-on-startup>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>AddNumberService</servlet-name>
  8. <url-pattern>/addNumberImpl</url-pattern>
  9. </servlet-mapping>
  10. <listener>
  11. <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  12. </listener>
  1. <servlet>
  2. <servlet-name>AddNumberService</servlet-name>
  3. <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
  4. <load-on-startup>1</load-on-startup>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>AddNumberService</servlet-name>
  8. <url-pattern>/addNumberImpl</url-pattern>
  9. </servlet-mapping>
  10. <listener>
  11. <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  12. </listener>

6.部署web项目 启动相应部署服务器。访问http://localhost:8080/TestJaxWebService/addNumberImpl?wsdl 这样每次启动web服务器就开放了一个web service的接口。

相关:

cxf:源码:https://github.com/apache/cxf

axis1 和 axis2 的简单使用

利用AXIS开发Webservice(一) —— 如何发布自己的webservice

Java WebService入门实例的更多相关文章

  1. WebService入门实例教程

    什么是WebService 通过使用WebService,您的应用程序可以向全世界发布信息,或提供某项功能,它是基于Web的服务,通过Web进行发布.查找和使用. WebService脚本平台需支持X ...

  2. 主题:Java WebService 简单实例

    链接地址:主题:Java WebService 简单实例    http://www.iteye.com/topic/1135747 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...

  3. java rmi 入门实例

    java rmi 入门实例 (2009-06-16 16:07:55) 转载▼ 标签: java rmi 杂谈 分类: java-基础    java rmi即java远程接口调用,实现了2台虚拟机之 ...

  4. Java AIO 入门实例(转)

    Java7 AIO入门实例,首先是服务端实现: 服务端代码 SimpleServer: public class SimpleServer { public SimpleServer(int port ...

  5. webservice入门实例,CXF方式

    1.下载CXF,及先关jar包. CXF 下载地址:http://cxf.apache.org/download.html,选择"File"列中的zip格式下载.解压后可以看到一些 ...

  6. Java WebService 简单实例

    前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作. 一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 ...

  7. Java WebService 简单实例[转]

    http://www.cnblogs.com/yisheng163/p/4524808.html?utm_source=tuicool 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必 ...

  8. Java WebService简单实例

    一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1.创建[Web Service Project],命名为[TheService ...

  9. Java WebService 简单实例[转载]

    [注意,本文转载自  http://hyan.iteye.com/    ] 一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1 ...

随机推荐

  1. 使用navicat连接mysql要报10038的错误

    1.mysql的设置 (1)授权mysql>grant all privileges on *.*  to  'root'@'%'  identified by 'youpassword'  w ...

  2. css 数字出现滚动条解决

    <div style="height:100px;overflow-y:auto;word-wrap:break-word;word-break:break-all;" ng ...

  3. matplotlib 基础

    plt.figure(2) #创建图表2 plt.figure(1) #创建图表1 ax1=plt.subplot(211) # 在上面 最近的 图表1上 创建子图1 ax2=plt.subplot( ...

  4. BZOJ 1115: [POI2009]石子游戏Kam

    1115: [POI2009]石子游戏Kam Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 883  Solved: 545[Submit][Stat ...

  5. css中import与link用法区别

    方式:引入CSS的方法有两种,一种是@import,一种是link @import url('地址');//注意,这种方式可以放在页面也可以放在css文件中<link href="地址 ...

  6. css中margin-top/margin-bottom失效

    要设置这两个值,我的理解应该在这个div的父容器中设置了固定宽高,或者设置了绝对定位,比如position:absolute(绝对定位) 或者压根不用,直接用padding-top/padding-b ...

  7. Visual Studio插件

    不定时更新,得到最好用的插件.(友情提示:安装插件时最好先备份系统) 1.Resharper 10.0.0.12.VS10x CodeMAP3.JavaScript Map Parser4.JScri ...

  8. 洛谷P2242 公路维修问题(Road)

    题目描述 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 牛棚一个紧挨着另一个被排成一行,牛就住在里面过夜. 有些牛棚里有牛 ...

  9. js实现A*寻路算法

    这两天在做百度前端技术学院的题目,其中有涉及到寻路相关的,于是就找来相关博客进行阅读. 看了Create Chen写的理解A*寻路算法具体过程之后,我很快就理解A*算法的原理.不得不说作者写的很好,通 ...

  10. primefaces p:dataExporter filename 支持中文 utf8

    p:fileDownload and p:dataExporter : for p:fileDownload, the Content-Disposition header should be set ...