最近一段时间研究webservice,一般来说,开发java的Webservice经常使用axis2和cxf这两个比较流行的框架

先使用cxf,开发一个完整示例,方便对webservice有一个整体的概念

使用的工具主要有eclipse、maven

一、开发服务端

整体结构如下:

1、创建maven的web工程,这个就不多赘述了,如果以前没搭建过可以去网上搜索下,网上资源很多

2、配置POM.xml文件,引入相应的jar包

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.moon.ws</groupId>
  5. <artifactId>cfxWSServer</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>cfxWSServer Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>3.8.1</version>
  15. <scope>test</scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.cxf</groupId>
  19. <artifactId>cxf-api</artifactId>
  20. <version>2.5.0</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.cxf</groupId>
  24. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  25. <version>2.5.0</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.apache.cxf</groupId>
  29. <artifactId>cxf-rt-bindings-soap</artifactId>
  30. <version>2.5.0</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.apache.cxf</groupId>
  34. <artifactId>cxf-rt-transports-http</artifactId>
  35. <version>2.5.0</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.apache.cxf</groupId>
  39. <artifactId>cxf-rt-ws-security</artifactId>
  40. <version>2.5.0</version>
  41. </dependency>
  42. </dependencies>
  43. <build>
  44. <finalName>cfxWSServer</finalName>
  45. </build>
  46. </project>

3、开发相关的接口和实现类

首先开发接口类

  1. package com.moon.cxfWebservice.server;
  2.  
  3. import javax.jws.WebParam;
  4. import javax.jws.WebService;
  5.  
  6. @WebService
  7. public interface Greeting {
  8. public String greeting(@WebParam(name="username")String userName);
  9. }

然后开发实现类

  1. package com.moon.cxfWebservice.server;
  2.  
  3. import java.util.Calendar;
  4.  
  5. import javax.jws.WebService;
  6.  
  7. @WebService(endpointInterface="com.moon.cxfWebservice.server.Greeting")
  8. public class GreetingImpl implements Greeting{
  9.  
  10. public String greeting(String userName) {
  11. return "Hello " + userName + ", currentTime is "
  12. + Calendar.getInstance().getTime();
  13. }
  14.  
  15. }

至此,服务端的代码就开发完成了。

4、配置web.xml和spring配置文件

web.xml配置如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <!-- 配置 Spring 配置文件的名称和位置 -->
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>/WEB-INF/config/spring.xml</param-value>
  11. </context-param>
  12. <!-- 启动 IOC 容器的 ServletContextListener -->
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16.  
  17. <!-- 配置字符集 -->
  18. <filter>
  19. <filter-name>encodingFilter</filter-name>
  20. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  21. <init-param>
  22. <param-name>encoding</param-name>
  23. <param-value>UTF-8</param-value>
  24. </init-param>
  25. <init-param>
  26. <param-name>forceEncoding</param-name>
  27. <param-value>true</param-value>
  28. </init-param>
  29. </filter>
  30. <filter-mapping>
  31. <filter-name>encodingFilter</filter-name>
  32. <url-pattern>/*</url-pattern>
  33. </filter-mapping>
  34. <servlet>
  35. <servlet-name>CXFServlet</servlet-name>
  36. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  37. <load-on-startup>1</load-on-startup>
  38. </servlet>
  39. <servlet-mapping>
  40. <servlet-name>CXFServlet</servlet-name>
  41. <!--==这个设置很重要,那么我们的webservice的地址就是http://localhost:8080/yourProgramName/webservice/Greeting=== -->
  42. <url-pattern>/webservice/*</url-pattern>
  43. </servlet-mapping>
  44.  
  45. <display-name>hello world!</display-name>
  46. <welcome-file-list>
  47. <welcome-file>index.html</welcome-file>
  48. <welcome-file>index.htm</welcome-file>
  49. <welcome-file>index.jsp</welcome-file>
  50. <welcome-file>default.html</welcome-file>
  51. <welcome-file>default.htm</welcome-file>
  52. <welcome-file>default.jsp</welcome-file>
  53. </welcome-file-list>
  54. </web-app>

spring的配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  9. http://cxf.apache.org/jaxws
  10. http://cxf.apache.org/schemas/jaxws.xsd">
  11. <!--=============== 实现类的bean,需要spring注入 ============================-->
  12. <bean id="greetingImpl" class="com.moon.cxfWebservice.server.GreetingImpl"/>
  13. <jaxws:endpoint id="greeting" implementor="#greetingImpl" address="/Greeting" />
  14.  
  15. </beans>

配置完这两个文件后,服务器端就完成了,我们就可以将这个服务跑起来了,跑起来后 访问   http://localhost:8080/yourProgramName/webservice/Greeting?wsdl

例如我本地的项目访问的就是

http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl

二、开发客户端

客户端调用我使用的两种方式

第一种使用apche cxf生成代码进行访问

1、下载apache cxf的包,地址为:http://cxf.apache.org/download.html 如:apache-cxf-3.1.6

2、解压apache-cxf-3.1.6到任意目录

3、配置环境变量

os系统设置

1)、export CXF_HOME=/Users/moon/Desktop/tools/apache-cxf-3.1.6

2)、path后面加    :$CXF_HOME/bin

windows系统设置

1)、CXF_HOME=D:\apache-cxf-3.1.6

2)、在path后面加上 %CXF_HOME%/bin;

在命令中输入wsdl2java,如果有提示usage,就表明配置成功

4、运行wsdl2java工具

在命令中输入:wsdl2java -d \xx\xxx\xx   -client http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl

(\xx\xxx\xx 是客户端程序代码所在的目录,http://localhost:8080/cxfWSServer/webservice/Greeting?wsdl 是发布的webservice服务)

附wsdl2java用法:

wsdl2java -p com -d D:\\src -all  xx.wsdl

-p  指定其wsdl的命名空间,也就是要生成代码的包名:

-d  指定要产生代码所在目录

-client 生成客户端测试web service的代码

-server 生成服务器启动web  service的代码

-impl 生成web service的实现代码

-ant  生成build.xml文件
-encoding utf-8 指定生成文件的编码方式

-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.

生成后的代码直接放到client工程上面

另外新建一个client类 直接使用生成的类调用

  1. package com.moon.cxf;
  2.  
  3. import com.moon.cxf.client.Greeting;
  4. import com.moon.cxf.client.GreetingImplService;
  5.  
  6. public class CxfClient {
  7. public static void main(String[] args) {
  8.  
  9. GreetingImplService serviceFactory = new GreetingImplService();
  10. Greeting service =
  11. serviceFactory.getGreetingImplPort();
  12.  
  13. String result = service.greeting("Jaune");
  14. System.out.println(result);
  15.  
  16. }
  17. }

二、使用axis调用webservice接口

引入axis 相关jar包

代码如下

  1. package com.moon.cxf;
  2.  
  3. import java.rmi.RemoteException;
  4.  
  5. import javax.xml.namespace.QName;
  6. import javax.xml.rpc.ParameterMode;
  7. import javax.xml.rpc.ServiceException;
  8.  
  9. import org.apache.axis.client.Call;
  10. import org.apache.axis.client.Service;
  11. import org.apache.axis.encoding.XMLType;
  12. /**
  13. * 使用axis调用cxf发布的webservice接口
  14. * @author moon
  15. *
  16. */
  17. public class AxisClient {
  18. public static void main(String[] args) throws ServiceException, RemoteException {
  19. try {
  20.  
  21. String endpoint = " http://localhost:8080/cfxWSServer/webservice/Greeting";
  22. // 调用过程
  23. Service service = new Service();
  24.  
  25. Call call = (Call) service.createCall();
  26.  
  27. call.setTargetEndpointAddress(new java.net.URL(endpoint));
  28.  
  29. call.setOperationName(new QName("http://server.cxfWebservice.moon.com/","greeting"));// WSDL里面描述的操作名称
  30.  
  31. call.addParameter("username",
  32. org.apache.axis.encoding.XMLType.XSD_STRING,
  33. javax.xml.rpc.ParameterMode.IN);// 操作的参数
  34.  
  35. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
  36.  
  37. call.setUseSOAPAction(true);
  38.  
  39. // 给方法传递参数,并且调用方法
  40. String temp = "good";
  41. Object[] obj = new Object[] { temp };
  42. String result = (String) call.invoke(obj);
  43.  
  44. System.out.println("Result is : " + result);
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. }

相关代码:https://github.com/15210448683/WebServiceDemoImpl

 
 
 

WebService(一)的更多相关文章

  1. webService

    什么是webService WebService,顾名思义就是基于Web的服务.它使用Web(HTTP)方式,接收和响应外部系统的某种请求.从而实现远程调用.  1:从WebService的工作模式上 ...

  2. 开始webservice了

    一.WebService到底是什么 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用 ...

  3. Spring WebService入门

    Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...

  4. 浅谈跨域以及WebService对跨域的支持

    跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源. 在 ...

  5. 浅谈WebService的版本兼容性设计

    在现在大型的项目或者软件开发中,一般都会有很多种终端, PC端比如Winform.WebForm,移动端,比如各种Native客户端(iOS, Android, WP),Html5等,我们要满足以上所 ...

  6. Atitit webservice发现机制 WS-Discovery标准的规范attilax总结

    Atitit webservice发现机制 WS-Discovery标准的规范attilax总结 1.1. WS-Discovery标准1 1.2. 一.WS-Discovery1 1.2.1.   ...

  7. java调用CXF WebService接口的两种方式

    通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...

  8. VS2010编写WebService与在IIS的发布<之简单讲解>

    工具VS2010,window环境win7 一:Webservice的创建与方法查看调用 1.新建空web应用程序项目 2.新建web服务 3.自动生成 4.直接跑起来,可以看到有2个方法 5.点击H ...

  9. webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口

    webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...

  10. webService学习之路(二):springMVC集成CXF快速发布webService

    继上一篇webService入门之后,http://www.cnblogs.com/xiaochangwei/p/4969448.html ,现在我将我周六在家研究的结果公布出来 本次集成是基于之前已 ...

随机推荐

  1. Linux用户查询、新增&删除

    1.查询用户tail -1 /etc/passwd 2.新增用户&用户组groupadd testgroup #组的添加useradd testuser #创建用户testuserpasswd ...

  2. charles 4.2.1 Ubuntu破解版安装

    charles 4.2.1 Ubuntu破解版安装 下载 charles-proxy-4.2.1_amd64.tar.gz 破解版 charles.jar 破解包 解压 sudo tar -zxvf ...

  3. pyhthon Opencv截取视频中的图片

    import os import cv2 ##加载OpenCV模块 def video2frames(pathIn='', pathOut='', imgname='', only_output_vi ...

  4. Linux下Ngnix的安装与配置

    由于我的博客项目在8084端口,需要Nginx来转发一下端口,记录一下安装过程和踩过的小坑. 一.下载 wget http://nginx.org/download/nginx-1.12.2.tar. ...

  5. 在Grafana使用普罗米修斯

    aaarticlea/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IE ...

  6. c语言 判断字符串长度 实现

    /* 首先明白答案的本质(该函数)是一个计数器该计数器用for循环来实现实现对一串字符串的计数字符串以空格开头 不计算空格 计算空格后的数字直到遇到\0结束.num计算器字符串不以空格结束 计算空格后 ...

  7. 函数this指向哪个对象?

    函数的this指向是根据函数调用时所处的执行环境来确定的. this指向对象的情况有四种: 1.使用new关键字时:this会绑定构造函数所创建的对象. function Foo(){ this.a ...

  8. oracle查询包含在子表中的主表数据

    Oracle数据库,查询某表中包含在子表中的数据,子表中数据按特定条件来源于该父表,SQL命令如 ) a_table父表,b_table子表,a和b表都有commandId列,a表的commandId ...

  9. .NET Core的SqlSugar上手使用小例子

    开始直接建个空的WEB项目-建Controllers文件夹-开启MVC-添加NuGet程序包SqlSugarCore public class Startup { // This method get ...

  10. C ProcessAsUser

    class Interop { public static void CreateProcess(string app, string path) { bool result; IntPtr hTok ...