webService:
Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。
Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。
Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。
Web Service是一种新的Web应用程序分支,其可以执行从简单的请求到复杂商务处理的任何功能。一旦部署以后,其他Web Service应用程序可以发现并调用它部署的服务。
因此,Web Service是构造分布式、模块化应用程序和面向服务应用集成的最新技术和发展趋势。
用于沟通不同平台,编程语言和组件模型中的不同的类型系统。
webservcie 的请求都是post的
webService使用的技术:
XMl:
可扩展的标记语言,是webService平台中表示数据的基本格式,易于建立和易于解析,与平台无关XML是由万维网协会(W3C)创建,
W3C制定的XML SchemaXSD 定义了一套标准的数据类型,并给出了一种语言来扩展这套数据类型
XML是由万维网协会(W3C)创建,W3C制定的XML SchemaXSD 定义了一套标准的数据类型,并给出了一种语言来扩展这套数据类型
XML可以使web services十分方便的处理数据,它的内容与表示的分离十分理想
SOAP:
SOAP即简单对象访问协议(Simple Object Access Protocol),它是用于交换XML(标准通用标记语言下的一个子集)编码信息的轻量级协议。
它有三个主要方面:XML-envelope为描述信息内容和如何处理内容定义了框架,
将程序对象编码成为XML对象的规则,
执行远程过程调用(RPC)的约定。
SOAP可以运行在任何其他传输协议上。
SOAP使用XML消息调用远程方法,这样web services可以通过HTTP协议的post和get方法与远程机器交互,而且,SOAP更加健壮和灵活易用
对于应用程序开发来说,使程序之间进行因特网通信是很重要的。目前的应用程序通过使用远程过程调用(RPC)在诸如 DCOM 与 CORBA 等对象之间进行通信,但是 HTTP 不是为此设计的。
RPC 会产生兼容性以及安全问题;防火墙和代理服务器通常会阻止此类流量。
通过 HTTP 在应用程序间通信是更好的方法,因为 HTTP 得到了所有的因特网浏览器及服务器的支持。SOAP 就是被创造出来完成这个任务的。
SOAP 提供了一种标准的方法,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。
SOAP 语法:
一条 SOAP 消息就是一个普通的 XML 文档,包含下列元素:
必需的 Envelope 元素,可把此 XML 文档标识为一条 SOAP 消息
可选的 Header 元素,包含头部信息
必需的 Body 元素,包含所有的调用和响应信息
可选的 Fault 元素,提供有关在处理此消息所发生错误的信息
本质上就是http:
Http协议:请求头和请求体之间必须有空行
post /XXX.action HTTP/1.1
HOST www.kay555.cn
contenType taxt/html;charset=utf8
id=111&name=xxx&....
SOAP协议:
post /XXX.action HTTP/1.1
HOST www.kay555.cn
contenType taxt/html;charset=utf8
<envelope>
<body>
</body>
</envelope>
WSDL
Web Service描述语言WSDL 就是用机器能阅读的方式提供的一个正式描述文档而基于XML(标准通用标记语言下的一个子集)的语言,用于描述Web Service及其函数、参数和返回值。
因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的。
一个 WSDL 文档的主要结构是类似这样的:
<definitions>
<types>
data type definitions........
</types>
<message>
definition of the data being communicated....
</message>
<portType>
set of operations......
</portType>
<binding>
protocol and data format specification....
</binding>
</definitions>
UDDI
UDDI 的目的是为电子商务建立标准;UDDI是一套基于Web的、分布式的、为Web Service提供的、信息注册中心的实现标准规范,
同时也包含一组使企业能将自身提供的Web Service注册,以使别的企业能够发现的访问协议的实现标准。
调用RPC与消息传递
Web Service本身其实是在实现应用程序间的通信。
应用程序通信的方法:RPC远程过程调用 和消息传递。
使用RPC的时候,客户端的概念是调用服务器上的远程过程,通常方式为实例化一个远程对象并调用其方法和属性。
RPC系统试图达到一种位置上的透明性:服务器暴露出远程对象的接口,而客户端就好像在本地使用的这些对象的接口一样,这样就隐藏了底层的信息,客户端也就根本不需要知道对象是在哪台机器上。
发布webservice服务方式:
@WebService
public class HelloService {
public String sayHello(String name) {
System.out.println("你的服务被调用该");
return "hello"+name;
}
//http://10.33.8.112:8080/hello?wsdl
//访问服务说明书形式
///http://10.33.8.112:8080/hello?xsd=1查看其中的方法和其中的参数
public static void main(String[] args) {
String address="http://10.33.8.112:8080/hello";
Object helloService = new HelloService();
Endpoint.publish(address, helloService);
}
}
客户端的调用:
jdk中wsimport的使用:解析wsdl生成客户端代码:
D:\test>wsimport -s . http://10.33.8.112:8080/hello?wsdl
解析出来生成java代码,然后创建代理对象,通过代理对象实现远程调用
/**
* 解析出来生成java代码,然后创建代理对象,通过代理对象实现远程调用
* @author leoi555
*
*/
public class Appclication {
public static void main(String[] args) {
HelloServiceService hsService=new HelloServiceService();
//创建代理对象
HelloService proxy=hsService.getHelloServicePort();
String ret=proxy.sayHello("555");
System.out.println(ret);
}
}
Apach CXF:Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。
CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,
采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。
cxf服务端:
导包:配置cxf.xml,书写接口和实现类。在web.xml配置:
<!-- 配置CXF提供的servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 通过初始化参数指定CXF配置文件位置 -->
<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>
在接口上添加注解@WebService然后提供对外访问的方法:
在cxf.xml配置:
<!--配置bean-->
<bean id="helloService" class="com.leo.service.HelloServiceImpl"></bean>
<!-- 注册服务
<url-pattern>/service/*</url-pattern>与下面访问的路径有关
http://ip:port/projectName/service/address
address对应的是下面的address的配置
-->
<jaxws:server id="myService" address="/hello">
<jaxws:serviceBean>
<ref bean="helloService"></ref>
</jaxws:serviceBean>
</jaxws:server>
cxf客户端:java project 版
通过wsimport -s . http://10.33.8.112:8080/hello?wsdl
解析出来生成java代码,然后创建代理对象,通过代理对象实现远程调用
把生成的代码中接口java文件引入:
配置cxf.xml:
<!-- 注册客户端代理对象,通过spring框架创建代理对象,使用代理对象实现远程调用 -->
<jaxws:client id="myclient"
address="http://10.33.8.112:8080/cxf_service/service/hello"
serviceClass="com.leo.client.HelloService"
> </jaxws:client>
测试代码:
/**
* 测试
* @author leoi555
*
*/
public class App {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("cxf.xml");
HelloService service=(HelloService) context.getBean("myclient");
String ret=service.sayHello("cxf:::");
System.out.println(ret);
}
}
cxf客户端也可以通过web项目创建
完整的cxf:引入jar包:
服
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>cxf_service</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>
<!-- 配置CXF提供的servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 通过初始化参数指定CXF配置文件位置 -->
<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>
</web-app>
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:beans="http://cxf.apache.org/configuration/beans"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/configuration/beans http://cxf.apache.org/schemas/configuration/cxf-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.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"/>
<!-- 注册bean -->
<bean id="helloService" class="com.leo.service.HelloServiceImpl"></bean>
<!-- 注册服务
<url-pattern>/service/*</url-pattern>与下面访问的路径有关
http://ip:port/projectName/service/address
address对应的是下面的address的配置
-->
<jaxws:server id="myService" address="/hello">
<jaxws:serviceBean>
<ref bean="helloService"></ref>
</jaxws:serviceBean>
</jaxws:server>
</beans>
书写接口并声明@WebService
@WebService
public interface HelloService {
public String sayHello(String name);
}
然后实现接口:
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
// TODO Auto-generated method stub
System.out.println("基于cxf的开发被调用了。。。。。");
return "hello"+name;
}
}
客户端:引入相应的jar包并通过wsimport 生成对应的服务端的java代码,然后把接口给引入到客户端:
通过wsimport -s . http://10.33.8.112:8080/hello?wsdl
解析出来生成java代码,然后创建代理对象,通过代理对象实现远程调用
把生成的代码中接口java文件引入:
<?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:beans="http://cxf.apache.org/configuration/beans"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/configuration/beans http://cxf.apache.org/schemas/configuration/cxf-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.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"/>
<!-- 注册客户端代理对象,通过spring框架创建代理对象,使用代理对象实现远程调用 -->
<jaxws:client id="myclient"
address="http://10.33.8.112:8080/cxf_service/service/hello"
serviceClass="com.leo.client.HelloService"
> </jaxws:client>
</beans>
/**
* 测试
* @author leoi555
*
*/
public class App {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("cxf.xml");
HelloService service=(HelloService) context.getBean("myclient");
String ret=service.sayHello("cxf:::");
System.out.println(ret);
}
}
然后可以看到客户端与服务端的控制台输出相应的数据:
其实webServiceJ就是别人给你个接口然后实现了@WebService注解然后你通过命令去访问他的wsdl然后即可以看到他对开发的方法,然后通过命令把他的的代码给解析出来,在客户端给引入这个接口然后去调用你想调用的方法即可:是与语言无关的平台无关的都可以进行交换数据,用于沟通不同平台,编程语言和组件模型中的不同的类型系统