cxf 与 spring整合

1、需要从cxf中添加如下jar包

neethi-3.0.2.jar

wsdl4j-1.6.3.jar

xmlschema-core-2.0.3.jar

cxf-2.6.10.jar

 

2、在web.xml中添加如下配置文件

<!-- 加载Spring容器配置 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext-server.xml</param-value>
</context-param>
 
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
 
<!-- 使用CXFServlet对象来处理对外发布的服务 -->
<servlet>
    <servlet-name>CXFService</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
 
<servlet-mapping>
    <servlet-name>CXFService</servlet-name>
    <url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

 

 

3、添加spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd">
	
	<!-- 下面的文件全部在cxf.jar目录下面 -->
	<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>

 

备注:

web应用加载类的两类路径:

1、在WEB-INF/class目录下

2、在WEB-INF/lib目录下面

这两种方式都是指向class的路径,在cxf.jar文件中有META-INF/cxf目录,里面有很多文件,因此资源引用可以使用:

classpath:META-INF/cxf/cxf.xml

 

4、写一个定制服务器接口

package adtec.ws.service;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import adtec.ws.model.User;

@WebService
@SOAPBinding(style = Style.RPC)
public interface IComplexUserService {
    
    public User getUserByName(@WebParam(name = "name") String name);
    
    public void setUser(User user);
}

 

5、webservice实现类

package adtec.ws.service.imp;


import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import adtec.ws.model.User;
import adtec.ws.service.IComplexUserService;

@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ComplexUserService implements IComplexUserService {
    
    public User getUserByName(@WebParam(name = "name") String name) {
        User user = new User();
        user.setId(111);
        user.setName(name);
        user.setAddress("china");
        user.setEmail(name + "@hoo.com");
        return user;
    }
    
    public void setUser(User user) {
        System.out.println("############Server setUser###########");
        System.out.println("setUser:" + user);
    }
}

 

6、model对象

package adtec.ws.model;

public class User {
	private int id;
	private String name;
	private String address;
	private String email;
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	private int age;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

7、在spring配置文件中添加如下代码

<!-- 
implementor 指定web service服务的提供者,支持两种方式:
1、直接给定服务器提供者的类名
2、设置为容器中一个bean(要在bean的id前面用#)
 
<jaxws:endpoint
	implementor="adtec.ws.service.imp.ComplexUserService"
	address="/hb"
>
-->
<bean id="complexUserService" class="adtec.ws.service.imp.ComplexUserService"></bean>
<jaxws:endpoint
	implementor="#complexUserService"
	address="/hb">
	<!-- 添加in拦截器 -->
	<jaxws:inInterceptors>
		<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
	</jaxws:inInterceptors>
	
	<jaxws:outInterceptors>
		<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
	</jaxws:outInterceptors>
</jaxws:endpoint>

 备注:在endpoid里面的拦截器可以不用使用。

 

8、启动tomcat

在地址栏输入 http://localhost:8080/myspring/webservice

或者在地址栏输入 http://localhost:8080/myspring/webservice/webservice?wsdl

你可能感兴趣的:(spring)