WebService(一)
最近一段时间研究webservice,一般来说,开发java的Webservice经常使用axis2和cxf这两个比较流行的框架
先使用cxf,开发一个完整示例,方便对webservice有一个整体的概念
使用的工具主要有eclipse、maven
一、开发服务端
整体结构如下:
1、创建maven的web工程,这个就不多赘述了,如果以前没搭建过可以去网上搜索下,网上资源很多
2、配置POM.xml文件,引入相应的jar包
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.moon.ws</groupId>
- <artifactId>cfxWSServer</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>cfxWSServer Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-api</artifactId>
- <version>2.5.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-rt-frontend-jaxws</artifactId>
- <version>2.5.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-rt-bindings-soap</artifactId>
- <version>2.5.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-rt-transports-http</artifactId>
- <version>2.5.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-rt-ws-security</artifactId>
- <version>2.5.0</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>cfxWSServer</finalName>
- </build>
- </project>
3、开发相关的接口和实现类
首先开发接口类
- package com.moon.cxfWebservice.server;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface Greeting {
- public String greeting(@WebParam(name="username")String userName);
- }
然后开发实现类
- package com.moon.cxfWebservice.server;
- import java.util.Calendar;
- import javax.jws.WebService;
- @WebService(endpointInterface="com.moon.cxfWebservice.server.Greeting")
- public class GreetingImpl implements Greeting{
- public String greeting(String userName) {
- return "Hello " + userName + ", currentTime is "
- + Calendar.getInstance().getTime();
- }
- }
至此,服务端的代码就开发完成了。
4、配置web.xml和spring配置文件
web.xml配置如下
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- 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_2_5.xsd">
- <!-- 配置 Spring 配置文件的名称和位置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/config/spring.xml</param-value>
- </context-param>
- <!-- 启动 IOC 容器的 ServletContextListener -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置字符集 -->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <!--==这个设置很重要,那么我们的webservice的地址就是http://localhost:8080/yourProgramName/webservice/Greeting=== -->
- <url-pattern>/webservice/*</url-pattern>
- </servlet-mapping>
- <display-name>hello world!</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
spring的配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <!--=============== 实现类的bean,需要spring注入 ============================-->
- <bean id="greetingImpl" class="com.moon.cxfWebservice.server.GreetingImpl"/>
- <jaxws:endpoint id="greeting" implementor="#greetingImpl" address="/Greeting" />
- </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类 直接使用生成的类调用
- package com.moon.cxf;
- import com.moon.cxf.client.Greeting;
- import com.moon.cxf.client.GreetingImplService;
- public class CxfClient {
- public static void main(String[] args) {
- GreetingImplService serviceFactory = new GreetingImplService();
- Greeting service =
- serviceFactory.getGreetingImplPort();
- String result = service.greeting("Jaune");
- System.out.println(result);
- }
- }
二、使用axis调用webservice接口
引入axis 相关jar包
代码如下
- package com.moon.cxf;
- import java.rmi.RemoteException;
- import javax.xml.namespace.QName;
- import javax.xml.rpc.ParameterMode;
- import javax.xml.rpc.ServiceException;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.apache.axis.encoding.XMLType;
- /**
- * 使用axis调用cxf发布的webservice接口
- * @author moon
- *
- */
- public class AxisClient {
- public static void main(String[] args) throws ServiceException, RemoteException {
- try {
- String endpoint = " http://localhost:8080/cfxWSServer/webservice/Greeting";
- // 调用过程
- Service service = new Service();
- Call call = (Call) service.createCall();
- call.setTargetEndpointAddress(new java.net.URL(endpoint));
- call.setOperationName(new QName("http://server.cxfWebservice.moon.com/","greeting"));// WSDL里面描述的操作名称
- call.addParameter("username",
- org.apache.axis.encoding.XMLType.XSD_STRING,
- javax.xml.rpc.ParameterMode.IN);// 操作的参数
- call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
- call.setUseSOAPAction(true);
- // 给方法传递参数,并且调用方法
- String temp = "good";
- Object[] obj = new Object[] { temp };
- String result = (String) call.invoke(obj);
- System.out.println("Result is : " + result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
相关代码:https://github.com/15210448683/WebServiceDemoImpl
WebService(一)的更多相关文章
- webService
什么是webService WebService,顾名思义就是基于Web的服务.它使用Web(HTTP)方式,接收和响应外部系统的某种请求.从而实现远程调用. 1:从WebService的工作模式上 ...
- 开始webservice了
一.WebService到底是什么 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用 ...
- Spring WebService入门
Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...
- 浅谈跨域以及WebService对跨域的支持
跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源. 在 ...
- 浅谈WebService的版本兼容性设计
在现在大型的项目或者软件开发中,一般都会有很多种终端, PC端比如Winform.WebForm,移动端,比如各种Native客户端(iOS, Android, WP),Html5等,我们要满足以上所 ...
- Atitit webservice发现机制 WS-Discovery标准的规范attilax总结
Atitit webservice发现机制 WS-Discovery标准的规范attilax总结 1.1. WS-Discovery标准1 1.2. 一.WS-Discovery1 1.2.1. ...
- java调用CXF WebService接口的两种方式
通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...
- VS2010编写WebService与在IIS的发布<之简单讲解>
工具VS2010,window环境win7 一:Webservice的创建与方法查看调用 1.新建空web应用程序项目 2.新建web服务 3.自动生成 4.直接跑起来,可以看到有2个方法 5.点击H ...
- webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口
webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...
- webService学习之路(二):springMVC集成CXF快速发布webService
继上一篇webService入门之后,http://www.cnblogs.com/xiaochangwei/p/4969448.html ,现在我将我周六在家研究的结果公布出来 本次集成是基于之前已 ...
随机推荐
- Linux用户查询、新增&删除
1.查询用户tail -1 /etc/passwd 2.新增用户&用户组groupadd testgroup #组的添加useradd testuser #创建用户testuserpasswd ...
- charles 4.2.1 Ubuntu破解版安装
charles 4.2.1 Ubuntu破解版安装 下载 charles-proxy-4.2.1_amd64.tar.gz 破解版 charles.jar 破解包 解压 sudo tar -zxvf ...
- pyhthon Opencv截取视频中的图片
import os import cv2 ##加载OpenCV模块 def video2frames(pathIn='', pathOut='', imgname='', only_output_vi ...
- Linux下Ngnix的安装与配置
由于我的博客项目在8084端口,需要Nginx来转发一下端口,记录一下安装过程和踩过的小坑. 一.下载 wget http://nginx.org/download/nginx-1.12.2.tar. ...
- 在Grafana使用普罗米修斯
aaarticlea/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IE ...
- c语言 判断字符串长度 实现
/* 首先明白答案的本质(该函数)是一个计数器该计数器用for循环来实现实现对一串字符串的计数字符串以空格开头 不计算空格 计算空格后的数字直到遇到\0结束.num计算器字符串不以空格结束 计算空格后 ...
- 函数this指向哪个对象?
函数的this指向是根据函数调用时所处的执行环境来确定的. this指向对象的情况有四种: 1.使用new关键字时:this会绑定构造函数所创建的对象. function Foo(){ this.a ...
- oracle查询包含在子表中的主表数据
Oracle数据库,查询某表中包含在子表中的数据,子表中数据按特定条件来源于该父表,SQL命令如 ) a_table父表,b_table子表,a和b表都有commandId列,a表的commandId ...
- .NET Core的SqlSugar上手使用小例子
开始直接建个空的WEB项目-建Controllers文件夹-开启MVC-添加NuGet程序包SqlSugarCore public class Startup { // This method get ...
- C ProcessAsUser
class Interop { public static void CreateProcess(string app, string path) { bool result; IntPtr hTok ...