CXF使用

一.服务端:

1.web.xml配置
<servlet>
  	<servlet-name>cxf</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  	<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>

 2.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" />
	<!-- 第一种发布服务方式 -->
	<!-- <bean id="helloService" class="com.zhou.service.impl.HelloServiceImpl"></bean>
	<jaxws:server id="myService" address="/cxfService">
		<jaxws:serviceBean>
			<ref bean="helloService"/>
		</jaxws:serviceBean>
	</jaxws:server> -->
	<!-- 第二种服务发布方式 -->
	<jaxws:endpoint address="/cxfService" implementor="com.zhou.service.impl.HelloServiceImpl">
	</jaxws:endpoint>
</beans>

  3.创建接口service和接口实现类serviceImpl,接口上加@WebService,访问地址:http://127.0.0.1:8080/CXF-server/service/cxfService?wsdl

import javax.jws.WebService;

@WebService
public interface HelloService {
	public String sayHello(String name);
}


import com.zhou.service.HelloService;

public class HelloServiceImpl implements HelloService{

	@Override
	public String sayHello(String name) {
		System.out.println("CXF服务端被调用...");
		return "hello"+name;
	}
}

  

  4.加入相应jia包

 

二.客户端

  1.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" /> -->
	
	<!-- 注册CXF客户端代理对象,通过spring框架创建这个代理对象,使用代理对象实现远程调用 -->
	<jaxws:client id="myClient" 
				address="http://192.168.0.158:8080/CXF-server/service/cxfService" 
				serviceClass="com.zhou.service.HelloService">
	</jaxws:client>
</beans>

  2.客户端代码

import javax.jws.WebService;

/**
 * targetNamespace对应http://+服务端包名
 * name对应类名
 * @author Administrator
 * @date{date}
 */
@WebService(targetNamespace = "http://service.zhou.com/", name = "HelloService")
// 接口名称可以不一样
public interface HelloService {
	// 方法名称、参数格式必须保持一致,否则无法找到服务的实现的方法
	public String sayHello(String name);
}

  

注:服务开就启动CXF服务需修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>crm_heima32</display-name> <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> <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> </web-app>

  

posted @ 2018-06-21 17:34  馳騁衿琾  阅读(189)  评论(0编辑  收藏  举报