webservice
约 1623 字大约 5 分钟
WebService是用来开发分布式的互操作应用程序
1. WebService介绍
Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,
可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么,
都可以相互交换数据
Web Service是自描述、 自包含的可用网络模块,可以执行具体的业务功能。
Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。
Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制
WebService通过HTTP POST方式接受客户的请求,实现两个系统之间的远程调用
WebService与客户端之间一般使用SOAP协议传输XML数据
它本身就是为了跨平台或跨语言而设计的
调用网络上的WebService服务http://webxml.com.cn/

2. SOAP和WSDL
SOAP(Simple Object Access Protocol):简单对象访问协议
SOAP作为一个基于XML语言的协议用于在网上传输数据。
SOAP = 在HTTP的基础上+XML数据。都是post请求
SOAP是基于HTTP的。!!!!!!!!!!!!!!!!
SOAP的组成如下:
Envelope – 必须的部分。以XML的根元素出现。
Headers – 可选的。
Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。
示例:
POST /WebServices/IpAddressSearchWebService.asmx HTTP/1.1
Host: ws.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getCountryCityByIp"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getCountryCityByIp xmlns="http://WebXml.com.cn/">
<theIpAddress>string</theIpAddress>
</getCountryCityByIp>
</soap:Body>
</soap:Envelope>
WSDL Web服务描述语言
WSDL(WebService Description Language):web 服务描述语言
就是一个xml文档,用于描述当前服务的一些信息(服务名称、服务的发布地址、服务提供的方法、方法的参数类型、方法的返回值类型等)
3. 基于jdk1.7发布一个WebService服务
服务端发布
第一步:创建一个Java项目
第二步:创建一个类,加入Webservice注解
第三步:提供一个方法sayHello
第四步:在main方法中调用jdk提供的发布服务的方法
第五步:访问服务的wsdl文档(服务的发布地址+?wsdl)http://192.168.115.87:8080/hello?wsdl
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class HelloService {
public String sayHello(String name,int i){
System.out.println("服务端的sayHello方法被调用了。。。。");
return "helle" + name;
}
public static void main(String[] args) {
String address = "http://192.168.115.87:8080/hello";
Object implementor = new HelloService();
Endpoint.publish(address, implementor);
}
}
客户端调用
//使用wsimport命令解析wsdl文件生成本地代码,把本地代码拷到项目中
//通过本地代码创建一个代理对象
//通过代理对象实现远程调用
public class App {
public static void main(String[] args) {
HelloServiceService ss = new HelloServiceService();
//创建客户端代理对象,用于远程调用
HelloService proxy = ss.getHelloServicePort();
String ret = proxy.sayHello("小明", 10);
System.out.println(ret);
}
}

4. apache CXF入门
官网:cxf.apache.org
Apache CXF = Celtix + Xfire
支持多种协议:SOAP1.1,1.2、XML/HTTP
CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS。C,c++,C#)
并可以与Spring进行快速无缝的整合,灵活的部署:可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面。
服务端开发
第一步:创建动态web项目
第二步:导入CXF相关jar包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.1</version>
</dependency>
第三步:在web.xml中配置CXF框架提供的一个Servlet
<!-- 配置CXF框架提供的Servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 通过初始化参数指定CXF框架的配置文件位置 默认cxf-servlet.xml-->
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:cxf.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
第四步:在类路径下提供cxf.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
</beans>
第五步:开发一个接口和实现类
@WebService
public interface HelloService {
public String sayHello(String name);
}
public class HelloServiceImpl implements HelloService{
public String sayHello(String name) {
System.out.println("基于CXF开发的服务端sayHello方法被调用了。。。。");
return "hello " + name;
}
}
第六步:在cxf.xml中注册服务
<bean id="helloService" class="com.itheima.service.HelloServiceImpl"/>
<!-- 注册服务 -->
<jaxws:server id="myService" address="/cxfService">
<jaxws:serviceBean>
<ref bean="helloService"/>
</jaxws:serviceBean>
</jaxws:server>
最后请求http://ip:port/projectName/service/cxfService
客户端开发
第一步:创建Java项目并导入CXF相关jar包
第二步:使用wsimport或者CXF提供wsdl2java生成本地代码,只需要生成接口文件
第三步:将接口文件复制到项目中
第四步:提供spring配置文件,注册客户端代理对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 注册CXF客户端代理对象,通过spring框架创建这个代理对象,使用代理对象实现远程调用 -->
<jaxws:client id="myClient"
address="http://192.168.115.87:8080/cxf_service/service/cxfService"
serviceClass="cn.itcast.client.HelloService">
</jaxws:client>
</beans>
第五步:读取spring配置文件,创建spring工厂,从工厂中获取代理对象,实现远程调用
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("cxf.xml");
HelloService proxy = (HelloService) ctx.getBean("myClient");
String ret = proxy.sayHello("test");
System.out.println(ret);
}
}