• cxf2.7.10 与 spring3.0.5集成


    开发环境:

    NetBeans7.4

    Tomcat 6.0.32

    一 服务端:

    1:新建JavaWeb工程 cxfspring-server,导入jar包如下图所示:

    2:在web.xml文件中添加如下配置项:

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-config.xml</param-value>
        </context-param>
        <!--监听spring-->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
    <!--监听CXFServlet-->
        <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>/ws/*</url-pattern>
        </servlet-mapping>

    3:新建一个com.test.server包,里面创建接口;新建一个com.test.server.impl包,里面实现接口,结构如下:

    (1):IArithmeticServer.java

    @WebService
    public interface IArithmeticServer {
        /**
         * 两个整数相加
         * @param opr1
         * @param opr2
         * @return 
         */
        public int addition(int opr1,int opr2);
        
        /**
         * 两个整数相减
         * @param opr1
         * @param opr2
         * @return 
         */
        public int subtraction(int opr1, int opr2);
    }

    (2):ArithmeticServerImp.java

    targetNamespace = "http://server.test.com/"一定要配置否则的话看这里:
    http://www.cnblogs.com/yshyee/p/3633537.html
    @WebService(
        endpointInterface = "com.test.server.IArithmeticServer",
        targetNamespace = "http://server.test.com/")
    public class ArithmeticServerImp implements IArithmeticServer{
    
        public int addition(int opr1, int opr2) {
            return opr1+opr2;
        }
        
        public int subtraction(int opr1, int opr2) {
            return opr1-opr2;
        }
        
    }

    (3):spring-config.xml

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        xsi:schemaLocation="http://www.springframework.org/schema/beans          
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context           
            http://www.springframework.org/schema/context/spring-context-3.0.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" />
        
        <!--一个endpoint对应于一个服务-->
        <jaxws:endpoint
            id="arithmeticServer"
            implementor="com.test.server.impl.ArithmeticServerImp"
            address="/ArithmeticServer"/>
        
        
    </beans>

    二 客户端:

    1:新建JavaWeb工程,cxf-spring-client,导入的jar包与服务端相同。

    2:新建一个测试类:通过动态方式调用CXF WebService。

    public class Test {
        public static void main(String args[]) throws Exception{
            fun1();
        }
        
        public static void fun1() throws Exception{
            JaxWsDynamicClientFactory clientFactory  = JaxWsDynamicClientFactory.newInstance();
            Client client = clientFactory.createClient("http://localhost:8080/cxfspring-server/ws/ArithmeticServer?wsdl");
            
            Object[] result = client.invoke("addition", 1,2);
            System.out.println("1+2:"+result[0]);
            
            result = client.invoke("subtraction", 1,2);
            System.out.println("1-2:"+result[0]);
        }
    }

     3:运行结果:

    信息: Created classes: com.test.server.Addition, com.test.server.AdditionResponse, com.test.server.ObjectFactory, com.test.server.Subtraction, com.test.server.SubtractionResponse
    1+2:3
    1-2:-1
    成功构建 (总时间: 3 秒)
  • 相关阅读:
    在.NET中读取嵌入和使用资源文件的方法
    T-SQL with关键字 with as 递归循环表
    IIS 部署WCF时遇到这么个错:
    WCF引用 代码
    C#中Windows通用的回车转Tab方法
    HTTP 错误 500.21
    如果你想开发一个应用(1-14)
    如果你想开发一个应用(1-13)
    如果你想开发一个应用(1-12)
    如果你想开发一个应用(1-11)
  • 原文地址:https://www.cnblogs.com/yshyee/p/3633684.html
Copyright © 2020-2023  润新知