服务端:
服务端和客户端都需要引入包
View Code
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <!-- 两种方式配置1.监听器配置 2.servlet配置, 以下的是采用监听器配置的 --> 8 9 <!-- 通过上下文参数指定spring配置文件的位置 --> 10 <context-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:cxf-servlet.xml</param-value> 13 </context-param> 14 <listener> 15 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 16 </listener> 17 18 <!-- 配置CXF框架的核心Servlet --> 19 <servlet> 20 <servlet-name>cxf</servlet-name> 21 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 22 <!-- 通过初始化参数指定配置文件的位置 --> 23 <!-- 24 <init-param> 25 <param-name>config-location</param-name> 26 <param-value>classpath:cxf-servlet.xml</param-value> 27 </init-param> 28 --> 29 </servlet> 30 31 <servlet-mapping> 32 <servlet-name>cxf</servlet-name> 33 <url-pattern>/cxf/*</url-pattern> 34 </servlet-mapping> 35 36 <welcome-file-list> 37 <welcome-file>index.jsp</welcome-file> 38 </welcome-file-list> 39 </web-app>
cxf-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 4 xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:jaxrs="http://cxf.apache.org/jaxrs" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://cxf.apache.org/bindings/soap 8 http://cxf.apache.org/schemas/configuration/soap.xsd 9 http://cxf.apache.org/jaxws 10 http://cxf.apache.org/schemas/jaxws.xsd 11 http://cxf.apache.org/jaxrs 12 http://cxf.apache.org/schemas/jaxrs.xsd 13 "> 14 <!-- 引入CXF Bean定义如下,早期的版本中使用,如果是servlet引入的话则下面三句不用了,因为框架引入了 --> 15 <import resource="classpath:META-INF/cxf/cxf.xml" /> 16 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 17 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 18 19 <!-- 通过spring配置文件发布CXF的服务 --> 20 21 <!-- 第一种发布方式:没有接口的发布(简单发布) --> 22 <!-- 23 id:唯一标识 24 address:访问url 25 implementor:提供服务的类型 26 --> 27 <jaxws:endpoint id="helloService" address="/hello" 28 implementor="cn.itcast.cxf.HelloService"> 29 <!-- 加入消息拦截器 --> 30 <jaxws:inInterceptors> 31 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> 32 </jaxws:inInterceptors> 33 <jaxws:outInterceptors> 34 <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 35 </jaxws:outInterceptors> 36 </jaxws:endpoint> 37 38 <!-- 第二种发布方式:带有接口的发布 --> 39 <!-- 40 id:唯一标识 41 address:访问url 42 serviceClass:接口类型 43 --> 44 <jaxws:server id="hiService" address="/hi" 45 serviceClass="cn.itcast.cxf.IHiService"> 46 <jaxws:serviceBean> 47 <!-- 提供服务的实现类 --> 48 <bean class="cn.itcast.cxf.HiServiceImpl"></bean> 49 </jaxws:serviceBean> 50 51 <!-- 加入消息拦截器 --> 52 <jaxws:inInterceptors> 53 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> 54 </jaxws:inInterceptors> 55 <jaxws:outInterceptors> 56 <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 57 </jaxws:outInterceptors> 58 </jaxws:server> 59 60 <!-- 配置restful方式的web服务 --> 61 <bean id="ps" class="cn.itcast.restful.PersonServiceImpl"></bean> 62 <jaxrs:server id="personService" address="/p"> 63 <jaxrs:serviceBeans> 64 <ref bean="ps"/> 65 </jaxrs:serviceBeans> 66 <jaxrs:inInterceptors> 67 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> 68 </jaxrs:inInterceptors> 69 <jaxrs:outInterceptors> 70 <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 71 </jaxrs:outInterceptors> 72 </jaxrs:server> 73 </beans>
忽略60-72 那是 jason方面的配置
IHiService.java
View Code
HiServiceImpl.java
1 package cn.itcast.cxf; 2 3 public class HiServiceImpl implements IHiService { 4 5 @Override 6 public String sayHi(String name) { 7 System.out.println("sayHi...."); 8 return "hi " + name; 9 } 10 11 }
然后
localhost:8080/项目地址/hi?xsdl
客户端
利用 wsimport -s 地址 或者ws2java -s地址的命令
得到文件后
把接口文件复制来
配置文件 ClientBean.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 4 xmlns:soap="http://cxf.apache.org/bindings/soap" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://cxf.apache.org/bindings/soap 8 http://cxf.apache.org/schemas/configuration/soap.xsd 9 http://cxf.apache.org/jaxws 10 http://cxf.apache.org/schemas/jaxws.xsd"> 11 <!-- 配置客户端bean --> 12 <!-- 13 id:唯一标识 14 address:请求的服务地址 15 serviceClass:客户端接口 16 --> 17 <jaxws:client id="hiService" address="http://localhost/CXF_03/cxf/hi" serviceClass="cn.itcast.cxf.IHiService"></jaxws:client> 18 19 </beans>
测试 IHiService就是拷贝过来的接口文件,放到项目中
1 package cn.itcast.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import cn.itcast.cxf.IHiService; 7 8 public class Test1 { 9 public static void main(String[] args) { 10 //初始化spring 11 ApplicationContext ctx = new ClassPathXmlApplicationContext("ClientBeans.xml"); 12 IHiService s = (IHiService) ctx.getBean("hiService"); 13 s.sayHi("abc"); 14 System.out.println(s.getClass().getName()); 15 } 16 }