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!

   

         

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值