zoukankan      html  css  js  c++  java
  • 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 秒)
  • 相关阅读:
    gRPC .NET Core跨平台学习
    .NET Core性能测试组件BenchmarkDotNet 支持.NET Framework Mono
    ASP.NET Core中间件(Middleware)实现WCF SOAP服务端解析
    gRPC C#学习
    中标麒麟关闭防火墙
    linux安装python
    python matplotlib.pyplot保存jpg图片失败
    python正态分布
    数据健康管理总结
    python使用statsmodel
  • 原文地址:https://www.cnblogs.com/yshyee/p/3633684.html
Copyright © 2011-2022 走看看