CXF_3

服务端代码:

WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	
	<!-- 通过上下文参数指定spring配置文件的位置 -->
	<!-- 以下配置服务启动时就加载配置文件,调用服务时无需重新加载  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:cxf-servlet.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>
		<!-- 通过初始化参数指定cxf配置文件的位置 -->
		<!-- 以下不注释则调用服务时会重新加载 -->
		<!-- 
		<init-param>
			<param-name>config-location</param-name>
			<param-value>classpath:cxf-servlet.xml</param-value>
		</init-param>
		 -->
	</servlet>
	<servlet-mapping>
		<servlet-name>cxf</servlet-name>
		<url-pattern>/cxf/*</url-pattern>
	</servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 /CXF_3/src/cxf-servlet.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-3.0.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">
	<!-- 引入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" />
	
	<!-- 第一种发布方式:简单发布(没有接口的发布) -->
	<!-- id:唯一标示  implementor:提供服务的类 address:服务的请求url-->
	<jaxws:endpoint id="helloService" implementor="cn.itcast.cxf.HelloService" address="/hello">
		<!-- 加入请求的消息拦截器 -->
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:inInterceptors>
		<!-- 加入响应的消息拦截器 -->
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:outInterceptors>
	</jaxws:endpoint>
	
	<!-- 第二种发布方式:带有接口的发布 -->
	<!-- id:唯一标示 serviceClass:接口类型 address:服务的请求url-->
	<jaxws:server id="hiService" serviceClass="cn.itcast.cxf.IHiService" address="/hi">
		<jaxws:serviceBean>
			<!-- 服务的实现类 -->
			<bean class="cn.itcast.cxf.HiServiceImpl"></bean>
		</jaxws:serviceBean>
		<!-- 加入请求的消息拦截器 -->
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:inInterceptors>
		<!-- 加入响应的消息拦截器 -->
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:outInterceptors>
	</jaxws:server>
	
	<jaxws:server id="personService" serviceClass="cn.itcast.cxf.service.IPersonService" address="/person">
		<jaxws:serviceBean>
			<!-- 服务的实现类 -->
			<bean class="cn.itcast.cxf.service.PersonServiceImpl"></bean>
		</jaxws:serviceBean>
		<!-- 加入请求的消息拦截器 -->
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:inInterceptors>
		<!-- 加入响应的消息拦截器 -->
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:outInterceptors>
	</jaxws:server>
</beans>

 /CXF_3/src/cn/itcast/cxf/domain/Person.java

package cn.itcast.cxf.domain;

public class Person {

	private String id;
	private String name;
	private String address;
	/**
	 * @return the id
	 */
	public final String getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public final void setId(String id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public final String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public final void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the address
	 */
	public final String getAddress() {
		return address;
	}
	/**
	 * @param address the address to set
	 */
	public final void setAddress(String address) {
		this.address = address;
	}
}

 /CXF_3/src/cn/itcast/cxf/service/IPersonService.java

package cn.itcast.cxf.service;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

import cn.itcast.cxf.domain.Person;

@WebService
@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
public interface IPersonService {

	public Person findPersonById(String id);
}

 /CXF_3/src/cn/itcast/cxf/service/PersonServiceImpl.java

package cn.itcast.cxf.service;

import cn.itcast.cxf.domain.Person;

public class PersonServiceImpl implements IPersonService {

	public Person findPersonById(String id) {
		System.out.println("findPersonById()...");
		Person p = new Person();
		p.setId(id);
		p.setAddress("aa");
		p.setName("jack");
		return p;
	}

}

 ---------------------------客户端代码------------------------------------------------------

/CXF_3_client/src/cn/itcast/cxf/service/App.java

package cn.itcast.cxf.service;

public class App {

	/**
	 * wsimport -s . http://localhost:8080/CXF_3/cxf/person?wsdl
	 */
	public static void main(String[] args) {
		IPersonServiceService ss = new IPersonServiceService();
		IPersonService is = ss.getIPersonServicePort();
		Person p = is.findPersonById("001");
		System.out.println(p.name);

	}

}

 

你可能感兴趣的:(CXF)