什么是WebService?
这里自己简单概括一下:可使用开放的XML标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。
Web Service技术, 不同机器,跨语言 ,可相互交换数据或集成。Web Service也很容易部署,他们之间用xml进行通讯,Web Service为业务流程的集成提供了一个通用机制。
WebService的特点:
1.WebService通过HTTP POST方式接受客户的请求
2.WebService与客户端之间一般使用SOAP协议传输XML数据
3.它本身就是为了跨平台或跨语言而设计的
4.网络上的WebService服务http://webxml.com.cn/
SOAP(Simple Object Access Protocol):简单对象访问协议
SOAP作为一个基于XML语言的协议用于在网上传输数据。
SOAP = 在HTTP的基础上+XML数据。
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文档,用于描述当前服务的一些信息(服务名称、服务的发布地址、服务提供的方法、方法的参数类型、方法的返回值类型等)
项目中的使用方法:
在实际在项目中几乎没有直接使用WebService的,都是结合框架来使用的。下面介绍一个常用的“apache CXF”。
apache CXF:
下载:
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项目环境搭建:
发布服务:
第一步:创建动态web项目
第二步:导入CXF相关jar包
第三步:配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置CXF框架提供的Servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</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>
第五步:针对t_customer表创建一个Customer客户实体类
id,name,phone,。。。。。。。。。自己定义,数据库同样
第六步:开发一个接口和实现类
第七步:配置cxf.xml
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///crm_xxx"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 支持事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注入service-->
<bean id="customerService" class="com.imooc.crm.service.CustomerServiceImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 注册服务 -->
<jaxws:server id="myService" address="/customer">
<jaxws:serviceBean>
<ref bean="customerService"/>
</jaxws:serviceBean>
</jaxws:server>
启动项目。
访问路径查看服务:ip+端口号+/项目名+/service
调用服务1:了解
第八步:生成本地代码
进到生成文件的目录,删除.class文件
第九步:把生成的目录复制到需要调用服务的项目中(放到源码包中src里面)
创建一个class调用服务
调用服务2:推荐使用
第一步:在maven项目的pom.xml中引入CXF的依赖
<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>
第二步:使用wsimport命令解析wsdl文件生成本地代码,只需要接口文件和实体类,将这两个实现类添加到项目中
第三步:在spring配置文件中注册crm客户端代理对象
<!-- 注册crm客户端代理对象 -->
<jaxws:client id="crmClient"
serviceClass="com.itheima.crm.ICustomerService"
address="http://192.168.0.168:8080/发布服务的项目名/service/customer"/>
第四步:通过@Autowired注解方式将代理对象注入给类,直接调用