4、webService CXF的应用2 Spring的配置方法

其实配置CXF方式挺多,这一章节,重点是通过tomcat启动时 加载WebService CXF 的方式。
好,下面 我们通过具体的代码来实现,

我这里用的是maven项目,首先pom.xml 的基础jar的引用

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.6</version>
        </dependency>
    </dependencies> 

配置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">

  <jaxws:endpoint id="calculator" address="http://10.0.5.154:8080/calculator" implementor="com.webservice.cxf.CalculatorServiceImpl" />
</beans>
接口的定义
package com.webservice.cxf;
import javax.jws.WebService;

@WebService
public interface CalculatorService {int add(int a, int b);
    String concat(String a, String b);
}
接口的实现
package com.webservice.cxf;
import javax.jws.WebService;
@WebService(endpointInterface="com.webservice.cxf.CalculatorService",serviceName="calculator")
public class CalculatorServiceImpl implements CalculatorService {
    public int add(int a, int b) {
        return a + b;
    }
    public String concat(String a, String b) {
        return a + b;
    }
}

通过main方法载 spring 配置 文件
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestwebSpring {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:*cxf-servers.xml");
    }
}
在浏览器中 输入 http://10.0.5.154:8080/calculator?wsdl 并出现以下页面时,则表示启动成功

通过代码测试
package com.webService.cxf;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
/**
 * Created by kangz on 2016/12/13.
 */
public class Textcxf {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //调用WebService
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(CalculatorService.class);
        factory.setAddress("http://localhost:8080/calculator");
        CalculatorService service = (CalculatorService) factory.create();
        System.out.println("[result]" + service.add(5,10));
    }
}

下面是小编的微信转帐二维码,小编再次谢谢读者的支持,小编会更努力的

----请看下方↓↓↓↓↓↓↓

百度搜索 Drools从入门到精通:可下载开源全套Drools教程

深度Drools教程不段更新中:


更多Drools实战陆续发布中………

扫描下方二维码关注公众号 ↓↓↓↓↓↓↓↓↓↓







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值