系列博文:
分布式.RPC调用-RMI & Hessian框架_闲猫的博客-CSDN博客
分布式.RPC-WebService三要素,三个规范, Soap协议_闲猫的博客-CSDN博客
分布式.RPC-WebService入门案例(java实现,注解实现,xsd文件解析,wsdl文件解析)_闲猫的博客-CSDN博客
分布式.RPC调用-RMI & Hessian框架_闲猫的博客-CSDN博客
目录
CXF是Apache公司产品,可以使用轻松实现WebService功能。
步骤:
代码:
JaxWsServerFactoryBean server [ww1] = new JaxWsServerFactoryBean();
server.setAddress[ww2] ("http://localhost:12345/member");
server.setServiceBean[ww3] (new MemberService());
server.create[ww4] ();
测试:

步骤:
1. 创建java工程 ,引入cxf的jar包
2. 生成本地代码
![]()
3. 调用远程服务
代码:
JaxWsProxyFactoryBean client = new JaxWsProxyFactoryBean();
client.setAddress("http://localhost:12345/member");
client.setServiceClass(MemberService.class);
MemberService memberService = (MemberService) client.create()[ww1] ;
Member member = memberService.getMember("");[ww2]
System.out.println(member.getName());
只需加注解@BindingType(SOAPBinding.SOAP12HTTP_BINDING),不需要引入扩展包。
这里发布两个模块服务。
步骤:(示例的配置只看格式,不看内容)
1. 创建web工程,添加cxf的jar包 和applicationContext.xml
2. 修改web.xml 添加spring监听
添加spring的监听器,并且配置配置文件扫描路径

3. 修改web.xml 添加CXFServlet控制器

4. 编写服务类
5. 编写applicationContext.xml
单个服务接口发布:

多个服务接口发布:
6. 测试

代码:
1. 创建web工程,添加cxf的jar包 和applicationContext.xml
工程:

applicationContext.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
[ww1] xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
[ww2] http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
beans>
2. 修改web.xml 添加spring监听
Spring监听器名称:org.springframework.web.context.ContextLoaderListener
将Spring配置文件配置在全局参数中:contextConfigLocation
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
listener-class>
listener>
3. 修改web.xml 添加CXFServlet控制器
CxfServlet控制器:org.apache.cxf.transport.servlet.CXFServlet
<servlet>
<servlet-name>cxfServletservlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
servlet-class>
servlet>
<servlet-mapping>
<servlet-name>cxfServletservlet-name>
<url-pattern>/ws/*url-pattern>
servlet-mapping>
4. 编写服务类
WeatherService服务类:(添加了@WebService注解的普通类)
@We bService
public class WeatherService {
public Weather getWeather(String str) {
return new Weather(123l, "赵菲");
}
}
MemberService这个服务类就不列了,一样。
5. 编写applicationContext.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="member"
class="cn.itcast.Service.member.MemberService" />
<bean id="weather"
class="cn.itcast.Service.weather.WeatherService"/>
<jaxws:server address="/member[ww1] ">
<jaxws:serviceBean>
<ref bean="member"/>
jaxws:serviceBean>
jaxws:server>
<jaxws:server address="/weather">
<jaxws:serviceBean>
<ref bean="weather"/>
jaxws:serviceBean>
jaxws:server>
beans>
[ww1]需要添加到servlet后边的
6. 测试

测试地址:
http://localhost:8080/server/ws/member?wsdl
http://localhost:8080/server/ws/weather?wsdl
地址组成:
server:项目名称
ws:servlet需要匹配的那个地址

member和 weather 配置的访问路径:

?wsdl 这个是访问wsdl的约定
附录:完整web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:applicationContext.xml
- </param-value>
- </context-param>
-
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
-
- <servlet>
- <servlet-name>cxfServlet</servlet-name>
- <servlet-class>
- org.apache.cxf.transport.servlet.CXFServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>cxfServlet</servlet-name>
- <url-pattern>/ws/*</url-pattern>
- </servlet-mapping>
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
-
- </web-app>
步骤:
1. 创建java工程 ,添加cxf的jar包 和applicationContext.xml
2. 生成本地代码
3. 编写applicationContext.xml

4. 编写代码调用本地代理

代码:
1. 创建java工程 ,添加cxf的jar包 和applicationContext.xml

2. 生成本地代码
将cmd的当前地址导在工程下的src目录,使用:wsimport –s . 地址 ,两次生成客户端代码。
wsimport –s . http://localhost:8080/server/ws/member?wsdl
wsimport –s . http://localhost:8080/server/ws/weather?wsdl
3. 编写applicationContext.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<jaxws:client
id="member[ww1] "
address="http://localhost:8080/server/ws/member?wsdl[ww2] "
serviceClass="cn.itcast.service.member.MemberService[ww3] "/>
<jaxws:client
id="weather"
address="http://localhost:8080/server/ws/weather?wsdl"
serviceClass="cn.itcast.service.weather.WeatherService"/>
beans>
4. 编写代码调用本地代理
public static void main(String[] args) {
ApplicationContext context [ww1] = new ClassPathXmlApplicationContext(
"applicationContext.xml");
WeatherService weatherService [ww2] = (WeatherService) context
.getBean("weather");
Weather weather = weatherService.getWeather("");[ww3]
System.out.println(weather.getName());
MemberService memberService = (MemberService) context.getBean("member");
Member member = memberService.getMember("");
System.out.println(member.getName());
}
END