项目6_ssh_bos_day7

什么是WebService

webservice就是两个系统之间的远程调用技术,webservice之间的调用可以实现跨语言调用,因为调用使用的是http协议,传输的数据格式为xml。

调用网络上的WebService服务

http://webxml.com.cn/

SOAP和WSDL概念

SOAP:简单对象访问协议     类似于SOAP = 在HTTP的基础上+XML数据

WSDL:Web服务描述语言

就是一个xml文档,用于描述当前服务的一些信息(服务名称、服务的发布地址、服务提供的方法、方法的参数类型、方法的返回值类型等)

基于jdk1.7发布一个WebService服务

第一步:创建一个Java项目

第二步:创建一个类,加入Webservice注解

第三步:提供一个方法sayHello

第四步:在main方法中调用jdk提供的发布服务的方法

第五步:访问服务的wsdl文档(服务的发布地址+?wsdl)http://192.168.115.87:8080/hello?wsdl

@WebService
public class HelloService {
	public String getName(String name){
		System.out.println("服务端的sayHello方法被调用了。。。。");
		return "Hello"+name;
	}

	public static void main(String[] args) {
		String address="http://192.168.0.29:8080/hello";
		Object implementor= new HelloService();;
		Endpoint.publish(address, implementor);
	}
}

客户端调用
jdk中wsimport命令使用

客户端调用
 1、使用wsimport命令解析wsdl文件生成本地代码
 2、通过本地代码创建一个代理对象
 3、通过代理对象实现远程调用

public class AppTest {
   public static void main(String[] args) {
        HelloServiceService service=new HelloServiceService();
	//创建客户端代理对象,用于远程调用
	HelloService proxy = service.getHelloServicePort();
	String name = proxy.getName("小郭");
	System.out.println(name);
    }
}

apache CXF入门(webservice框架)

入门案例(服务端开发)

第一步:创建动态web项目
第二步:导入CXF相关jar包
第三步:在web.xml中配置CXF框架提供的一个Servlet

<!-- 配置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>

第四步:在类路径下提供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>

第五步:开发一个接口和实现类

第六步:在cxf.xml中注册服务

发布到网上:http://ip:端口/项目名称/service/cxfservice(我们只能确定address的数据)

那么我这个项目发布到网上的地址是http://192.168.0.29:8080/cxf_webservice/service/cxfservice?wsdl

入门案例(客户端开发)

方式一:使用jdk提供的wsimport命令生成本地代码完成调用(和上面的webservice没有区别,因此省略)

方式二:使用CXF提供的方式(重点)
第一步:创建Java项目并导入CXF相关jar包
第二步:使用wsimport或者CXF提供wsdl2java生成本地代码,只需要生成接口文件

问题:在控制台输入wsdl2java会发现,有问题,需要配置环境变量,比较麻烦,因此,直接去这个apache-cxf-2.4.2所在的位置执行该命令即可

效果如图:

生成本地代码的命令

第三步:将接口文件复制到项目中

第四步:提供spring配置文件,注册客户端代理对象(将cxf.xml文件复制到src下)

<jaxws:client id="myClient" address="http://192.168.0.29:8080/cxf_webservice/service/cxfservice" 
serviceClass="cn.itcast.client.HelloService"></jaxws:client>

第五步:读取spring配置文件,创建spring工厂,从工厂中获取代理对象,实现远程调用

基于CXF开发crm服务

数据库环境搭建(执行数据库脚本)

web项目环境搭建

第一步:创建动态web项目

第二步:导入CXF相关jar包

第三步:配置web.xml

  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
   <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:cxf.xml</param-value>
  </context-param>

  <!-- 配置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>

和以前的区别就是将发布的时机提前了

配置spring的监听器。配置不配置都可以

区别就是:配置的话,由监听器去加载cxf.xml文件

项目启动的时候这个服务就已经发布出去了

不配置的话,项目启动的时候,没有发布这个服务,而是在第一次访问的时候才发布

第四步:在类路径下提供cxf.xml

第五步:针对t_customer表创建一个Customer客户实体类

/**
 * 物流公司的客户实体
 */
public class Customer {
  private int id ;
  private String name ;
  private String station ;
  private String telephone ;
  private String address ;
  private String decidedzone_id ;
//提供空参数的构造方法和有参的构造方法
}

第六步:开发一个接口和实现类

第七步:配置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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
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
					http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.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 id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql:///crm_heima32"/>
		<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>
	
	<bean id="customerService" class="com.itheima.crm.service.CustomerServiceImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"/>
	</bean>
	
	<!-- 注册服务 -->
	<jaxws:server id="myService" address="/customer">
		<jaxws:serviceBean>
			<ref bean="customerService"/>
		</jaxws:serviceBean>
	</jaxws:server>
</beans>

启动项目后,出现此效果,说明配置成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

guoyebing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值