北在北方

太白枝头看,花开不计年,杯中浮日月,楼外是青天。
随笔 - 200, 文章 - 0, 评论 - 239, 阅读 - 68万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Web项目中使用Spring整合CXF发布Web Services

Posted on   CN.programmer.Luxh  阅读(1536)  评论(10编辑  收藏  举报

  Spring的Web项目搭建就不再啰嗦了,直接说整合的关键步骤。

  1、CXF的包需要导入到项目中

  2、web.xml文件添加下面的内容

复制代码
<servlet>
        <servlet-name>cxf</servlet-name>
        <display-name>cxf</display-name>
        <description>Apache CXF Endpoint</description>
        <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>/services/*</url-pattern>
    </servlet-mapping>
复制代码

  3、提供服务的接口和实现类

    1)、接口

复制代码
package cn.luxh.app.ws;

import javax.jws.WebService;

@WebService
public interface Calculator {
    
    int add(int num1,int num2);
}
复制代码

    2)实现类

复制代码
package cn.luxh.app.ws;

import javax.jws.WebService;

@WebService(endpointInterface="cn.luxh.app.ws.Calculator")
public class CalculatorImpl implements Calculator{

    @Override
    public int add(int num1, int num2) {
        return num1 + num2;
    }

}
复制代码

  3、在spring的配置文件中添加如下内容

    1)首先命名空间添加

    和xsi:schemaLocation=后面添加

http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

    2)接着配置提供服务的endpoint

    <jaxws:endpoint id="calculator" 
                        implementor="cn.luxh.app.ws.CalculatorImpl" 
                        address="/Calculator" />

  4、就这么简单,就可以发布一个Web Service服务了,看来CXF和Spring是完美的结合。

  5、启动Web应用,访问:http://localhost:8080/CXFAPP/services/Calculator?wsdl 就可以看到wsdl的描述文件了。CXFAPP是我的web应用名称。

 

点击右上角即可分享
微信分享提示