cxf与spring集成

         Spring框架在java开发中随处可见,带来了spring与cxf集成问题.cxf提供和spring无缝整合。

          1、新建一个web项目,把spring jar和cxf jar 拷贝至webapp 的lib目录下

 

           spring jar列表

         

            spring-aop-3.0.5.RELEASE.jar
            spring-asm-3.0.5.RELEASE.jar
            spring-beans-3.0.5.RELEASE.jar
            spring-context-3.0.5.RELEASE.jar
            spring-core-3.0.5.RELEASE.jar
            spring-expression-3.0.5.RELEASE.jar
            spring-jms-3.0.5.RELEASE.jar
            spring-tx-3.0.5.RELEASE.jar
            spring-web-3.0.5.RELEASE.jar

   cxf jar列表

   

         commons-logging-1.1.1.jar
         cxf-2.3.11.jar
         neethi-2.0.5.jar
         wsdl4j-1.6.2.jar
         XmlSchema-1.4.7.jar

Map与String类型工具类所在jar

        xstream-1.3.jar

     2、修改web.xml文件

        

        <!-- spring配置文件  --> 
      <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
		WEB-INF/spring/applicationContext.xml,
		WEB-INF/spring/cxf.xml
		</param-value>
	</context-param>
     <!-- 加载spring -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

        <!--处理cxf请求  -->
      	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<display-name>CXF Servlet</display-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

    3、修改cxf.xml文件

    

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/lang 
         http://www.springframework.org/schema/lang/spring-lang.xsd
         http://cxf.apache.org/jaxws      
         http://cxf.apache.org/schemas/jaxws.xsd
         ">
 
         <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="helloServiceImp" class="com.skydream.cxf.impl.HelloServiceImpl"></bean>
          -->
          <jaxws:endpoint  implementor="com.skydream.cxf.impl.HelloServiceImpl"
             address="/hello">
               
          </jaxws:endpoint>
   
</beans>

中间三行import是导入cxf jar中的配置,但是有时我不需要也能启动,有时却不行,不知道为什么?

Jaxws:endpoint

   implements : 服务类

  address:服务访问路径(因为是web 服务,前面自动补充根路径)

   服务接口

@WebService
public interface IHelloService {

	@WebMethod
	String sayHello(String name);
	
	@WebResult(name="listPerson")
	List<Person> getPersionByName(@WebParam(name="names")String[] names);
	
	@XmlJavaTypeAdapter(MapAdapter.class)
	Map<String,Person> getPerson(@XmlJavaTypeAdapter(MapAdapter.class)Map<String,Person> person);
}

服务实现类

 

@WebService(endpointInterface="com.skydream.cxf.IHelloService",serviceName="helloService")
public class HelloServiceImpl implements IHelloService 
{
	private final List<Person> lstPerson = new ArrayList<Person>();
	
	private final Map<String,Person> objMap = new HashMap<String, Person>();

	public HelloServiceImpl()
	{
		Person objPeron1 = new Person();
		objPeron1.setId("001");
		objPeron1.setName("skydream");
		objPeron1.setAge(25);
		lstPerson.add(objPeron1);
		objMap.put(objPeron1.getId(),objPeron1);
		
		Person objPeron2 = new Person();
		objPeron2.setId("002");
		objPeron2.setName("xuhaibo");
		objPeron2.setAge(24);
		lstPerson.add(objPeron2);
		objMap.put(objPeron2.getId(),objPeron2);
		
		Person objPeron3 = new Person();
		objPeron3.setId("003");
		objPeron3.setName("xufuyu");
		objPeron3.setAge(25);
		lstPerson.add(objPeron3);
		objMap.put(objPeron3.getId(),objPeron3);
	}
	
	@Override
	public String sayHello(String name) 
	{
		return "Hello "+name+" ;Now time is "+new Date();
	}

	@Override
	public List<Person> getPersionByName(String[] names) {
		
		List<Person> persons = new ArrayList<Person>();
		for(Person objPerson : lstPerson)
		{
			if(java.util.Arrays.asList(names).contains(objPerson.getName()))
			{
				persons.add(objPerson);
			}
		}
		return persons;
	}

	@Override
	public Map<String, Person> getPerson(Map<String, Person> person) {
		person = objMap;
		return person;
	}

}

4、启动web服务器。访问http://localhost:8080/cxf_spring/services/hello?wsdl可以看到wsdl,就说明服务发布成功了。

补充说明:如果这是想在服务器端加拦截器,如何操作呢?

  <jaxws:endpoint  implementor="com.skydream.cxf.impl.HelloServiceImpl"
             address="/hello">
                <!--配置了2个进入拦截器,一个内置日志拦截器、一个自定义拦截器 -->
                 <jaxws:inInterceptors>
                    <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
                    <bean class="com.skydream.cxf.interceptor.DefineInterceptor">
                       <constructor-arg>
                         <value>send</value>
                       </constructor-arg>
                    </bean>
                 </jaxws:inInterceptors>
               <!--配置一个出去拦截器 -->
                 <jaxws:outInterceptors>
                    <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
                 </jaxws:outInterceptors>
          </jaxws:endpoint>

ok!

   

         

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *