CXF与Spring集成的简单配置

CXF +Spring 整合下的Webservice。
部署环境:tomcat 6.0
文件目录结构:
这里写图片描述
web.xml(Spring MVC相关配置可忽略,重点在于Spring ContextLoaderListener与CXFServlet)

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- Spring contextLoaderListener params -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
                classpath:spring/*.xml
                classpath:cxf-servlet.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 牛气冲天的Spring Encoding Filter -->
<filter>
    <filter-name>SpringEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceRequestEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SpringEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 威武霸气SpringMVC DispatcherServlet-->
<servlet>
    <servlet-name>SpringmvcDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>SpringmvcDispatcherServlet</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

<!-- CXF webservice -->
<servlet>
    <servlet-name>CXFServlet</servlet-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>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>

相当重要的cxf-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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://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-servlet.xml" />


    <!-- 
        另一种方式
        <bean id="hello" class="demo.spring.service.HelloWorldImpl" />
        <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
     -->
    <!--服务端发布(使用配置方式发布WebService)  -->
    <jaxws:endpoint id="helloWorld" implementor="com.ainoties.core.webservice.sample.impl.HelloCXFImpl"
        address="/HelloWorld" />

    <!--客户端调用 -->
    <jaxws:client id="helloWorldClient" serviceClass="com.ainoties.core.webservice.sample.HelloCXF" 
        address="http://localhost:8080/cxf/webservice/HelloWorld"></jaxws:client> 


</beans> 

接口类:HelloCXF

@WebService
public interface HelloCXF {

    public void sayHi(String username);

    public void hiUser(User user);
}

实现类,也是服务类

@WebService(
        endpointInterface="com.ainoties.core.webservice.sample.HelloCXF",
        serviceName="helloworld",
        targetNamespace="http://localhost:8080/cxf/webservice/HelloCXF"
)
public class HelloCXFImpl implements HelloCXF {

    @Override
    public void sayHi(String username) {
        // TODO Auto-generated method stub
        System.out.println("hello "+username);
    }
    @Override
    public void hiUser(User user) {
        System.out.println(user);
    }
}

客户端API

public class HelloCXFTest {

    public static void main(String[] args) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setServiceClass(HelloCXF.class);
        factory.setAddress("http://localhost:8080/cxf/webservice/HelloWorld");
        HelloCXF client = (HelloCXF) factory.create();
        User user =new User();
        user.setUsername("许小宝");
        user.setPassword("password");
        client.sayHi("HI");
        client.hiUser(user);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值