CFX构建webservice实例,与Spring整合.

项目结构图:
CFX构建webservice实例,与Spring整合.

步骤一: 添加maven包依赖

CFX构建webservice实例,与Spring整合.
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cfx.ws</groupId>
    <artifactId>ws-cfx-core</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>ws-cfx-core</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>3.1.1.RELEASE</org.springframework.version>
        <cxf.version>2.7.1</cxf.version>
        <junit.version>4.8.1</junit.version>
    </properties>

    <dependencies>
        <!-- SPRINGFRAMEWORK -->
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-expression</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-beans</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aop</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context-support</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-tx</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  

        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-orm</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-oxm</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  

        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
          
      
        
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc-portlet</artifactId>  
            <version>${org.springframework.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
            <version>${org.springframework.version}</version>  
            <scope>test</scope>  
        </dependency>  
        
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-security</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-policy</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-bundle-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>1.1.1</version>
        </dependency>
                 
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>ws-cxf-core</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <!-- cause we use tomcat , so follow plugin no use.
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <configuration>
                    <contextPath>/</contextPath>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>9000</port>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            
        </plugins>
        -->
    </build>

</project>
View Code

步骤二:添加CFX核心过滤到web.xml文件

CFX构建webservice实例,与Spring整合.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ws-beans-*.xml</param-value>
    </context-param>
    
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

</web-app>  
CFX构建webservice实例,与Spring整合.

步骤三创建ws 接口并实现
WS接口

CFX构建webservice实例,与Spring整合.
package com.ws.cfx.welc;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.ws.cfx.welc.model.WorkInfo;
/*
 * ws 接口 
 */
@WebService
public interface WelcomeWS {

    @WebMethod
    @WebResult String welcomeMessage(@WebParam String message);
    
    @WebMethod
    @WebResult WorkInfo workInfoNode(Long id);
}
CFX构建webservice实例,与Spring整合.

 

接口实现类

CFX构建webservice实例,与Spring整合.
package com.ws.cfx.welc.impl;

import java.util.Date;
import com.ws.cfx.welc.WelcomeWS;
import com.ws.cfx.welc.model.WorkInfo;
/**
 * 
 * @author dennisit@163.com
 *
 */
public class WelcomeWSImpl implements WelcomeWS{

    
    @Override
    public String welcomeMessage(String message) {
        return "server success , accpet message values is :" + message;
    }

    @Override
    public WorkInfo workInfoNode(Long id) {
        WorkInfo workInfo = new WorkInfo();
        workInfo.setId(2L);
        workInfo.setWorkname("Node 2");
        workInfo.setWorktime(new Date());
        return workInfo;
    }

}
CFX构建webservice实例,与Spring整合.

步骤四 实例化cfx 服务端与客户端到配置文件
cfx服务端配置文件ws-beans-server.xml

CFX构建webservice实例,与Spring整合.
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!--
        手动添加的内容:
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        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" />

    <jaxws:endpoint id="welcomews" implementor="com.ws.cfx.welc.impl.WelcomeWSImpl"
        address="/welcome" />

</beans>
CFX构建webservice实例,与Spring整合.

 

 

 

cfx客户端配置文件ws-beans-client.xml

CFX构建webservice实例,与Spring整合.
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!--
        手动添加的内容:
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        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="welcomeWsclient" class="com.ws.cfx.welc.WelcomeWS" factory-bean="clientFactory" factory-method="create" />

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.ws.cfx.welc.WelcomeWS" />
        <property name="address" value="http://localhost:8080/ws-cfx-core/ws/welcome" />
    </bean>
</beans>
CFX构建webservice实例,与Spring整合.

 

步骤五编写测试类

CFX构建webservice实例,与Spring整合.
package com.ws.cfx.welc;
import java.net.SocketTimeoutException;

import javax.xml.ws.WebServiceException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ws.cfx.welc.WelcomeWS;
import com.ws.cfx.welc.model.WorkInfo;

public class WscxfTest {

    ApplicationContext context = null;
    
    @Before
    public void init(){
        context = new ClassPathXmlApplicationContext("classpath:ws-beans-server.xml","classpath:ws-beans-client.xml");
        System.out.println("context init result : " + (context!=null? true:false) );
    }
    
    @Test
    public void cfxServer(){
        
        WelcomeWS welcome =  (WelcomeWS) context.getBean("welcomeWsclient");
        System.out.println(welcome.welcomeMessage("This if first cfx ws ."));
        try {  
            WorkInfo workInfo = welcome.workInfoNode(2L);
            System.out.println(workInfo); 
        } catch(Exception e) {  
            if (e instanceof WebServiceException   
                    && e.getCause() instanceof SocketTimeoutException) {  
                System.err.println("timeout exception.");  
            } else {  
                e.printStackTrace();  
            }  
        }  
    }
}
CFX构建webservice实例,与Spring整合.

步骤六启动tomcat服务器,访问路径http://localhost:8080/ws-cfx-core/ws/welcome?wsdl,测试server.

运行测试类,执行结果如下

CFX构建webservice实例,与Spring整合.
context init result : true
server success , accpet message values is :This if first cfx ws .
com.ws.cfx.welc.model.WorkInfo@3ee62bde[id=2,workname=Node 2,worktime=Thu Apr 03 12:02:16 GMT+08:00 2014]
CFX构建webservice实例,与Spring整合.


转载请注明出处:[http://www.cnblogs.com/dennisit/p/3649964.html]

CFX构建webservice实例,与Spring整合.,布布扣,bubuko.com

CFX构建webservice实例,与Spring整合.

上一篇:javascript 获取最终样式的函数


下一篇:java多线程与线程并发四:线程范围内的共享数据