Fork me on GitHub

CXF+SOAP Service+Spring整合

环境:apache-cxf-2.7.4+spring 3.2 使用soap1.2协议
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>CXF_DEMO</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-server.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXFServlet</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>/ws/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

applicationContext-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.2.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.2.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.2.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-servlet.xml" />
    <!-- 发布服务 -->
    <jaxws:server address="/helloService" serviceClass="cn.base.webservice.server.IHelloService">
        <jaxws:serviceBean>
            <bean class="cn.base.webservice.server.HelloServiceImpl"></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>

客服端
applicationContext-client.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.2.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.2.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.2.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-servlet.xml" />

    <!-- 客户端服务 -->
    <bean id="client" class="cn.base.webservice.server.IHelloService" factory-bean="clientFactory" factory-method="create" />
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="cn.base.webservice.server.IHelloService" />
        <property name="address" value="http://localhost:8080/CXF_DEMO/ws/helloService" />
    </bean>
</beans>

code

IHelloService
@WebService(targetNamespace = "http://www.cnblogs.com/")
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface IHelloService {
    public abstract String sayHello(String username);
    public abstract String sayGoodBye(String username);
}
IHelloServiceImpl
@WebService(targetNamespace="http://www.cnblogs.com/")
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public class HelloServiceImpl implements IHelloService {
    public String sayHello(String username) {
        return "hello ," + username;
    }

    public String sayGoodBye(String username) {
        return "goodbye ," + username;
    }
}
HelloServiceClient
public class HelloServiceClient {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
        IHelloService helloService = (IHelloService) context.getBean("client");
        System.out.println(helloService.sayHello("Irving"));
        System.out.println(helloService.sayGoodBye("TOM"));
    }
}

访问:http://localhost:8080/CXF_DEMO/ws/

Available SOAP services:

IHelloService
  • sayHello
  • sayGoodBye
Endpoint address: http://localhost:8080/CXF_DEMO/ws/helloService
WSDL : {http://www.cnblogs.com/}IHelloServiceService
Target namespace: http://www.cnblogs.com/

Available RESTful services:

Refer:http://814318774.iteye.com/blog/1844481

posted @ 2013-05-04 15:50  花儿笑弯了腰  阅读(446)  评论(0编辑  收藏  举报