使用cxf在springMVC环境搭建webservice示例-服务端

3 篇文章 0 订阅
2 篇文章 0 订阅

1、 添加依赖

添加cxf的必要依赖 springMVC相关的依赖自行添加

<dependency>
  <groupId>org.codehaus.woodstox</groupId>
  <artifactId>woodstox-core-asl</artifactId>
  <version>4.4.1</version>
</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>

<properties>
  <cxf.version>3.0.4</cxf.version>
</properties>

2、 web.xml配置

这里提供了webservice的必要配置,具体springMVC的内容暂未加入

所有webservice接口配置放置于bean.xml,具体内容见第3节

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:beans.xml</param-value>
</context-param>

<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

<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>/cxf/*</url-pattern>
</servlet-mapping>

3、 webservice bean配置

<?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">

  <!-- cxf必要配置 -->
  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

  <jaxws:endpoint
                  id="reportEntity"
                  implementor="skytech.casedemo.ws.ReportEntityServiceImpl"
                  address="/findReportEntity"></jaxws:endpoint>

</beans>

4、 接口定义与实现

  • ReportEntity.java

    package skytech.casedemo.ws;
    
     /**
      * 直报主体定义
      * Created by ht on 2017/1/20.
      */
     public class ReportEntity {
    
         private String pkid;
         private String enterpirseName;
         private String orgaizationId;
    
         public String getPkid() {
             return pkid;
         }
    
         public void setPkid(String pkid) {
             this.pkid = pkid;
         }
    
         public String getEnterpirseName() {
             return enterpirseName;
         }
    
         public void setEnterpirseName(String enterpirseName) {
             this.enterpirseName = enterpirseName;
         }
    
         public String getOrgaizationId() {
             return orgaizationId;
         }
    
         public void setOrgaizationId(String orgaizationId) {
             this.orgaizationId = orgaizationId;
         }
     }
  • ReportEntityService.java

 package skytech.casedemo.ws;

 import javax.jws.WebService;

/**
* 接口定义
*  @WebService用于定义webservice对外开放的接口
* Created by ht on 2017/1/20.
*/
@WebService
public interface ReportEntityService {

   ReportEntity findOne(String pkid);

}
  • ReportEntityServiceImpl.java
package skytech.casedemo.ws;

     /**
      * Created by ht on 2017/1/20.
      * webservice接口实现
      */
     public class ReportEntityServiceImpl implements ReportEntityService {

         public ReportEntityServiceImpl() {
             System.out.println("初始化ReportEntityServiceImpl");
         }

         @Override
         public ReportEntity findOne(String pkid) {

             ReportEntity reportEntity = new ReportEntity();
             reportEntity.setPkid(pkid);
             reportEntity.setEnterpirseName(pkid + "_测试的直报主体");
             reportEntity.setOrgaizationId("12322222222");
             return reportEntity;
         }

     }

5、启动服务

启动ide,配置tomcat等相关参数(例:端口8080,上下文ws)后启动。

使用浏览器访问:

接口列表:http://localhost:8080/ws/cxf

这里写图片描述

wsdl接口描述:http://localhost:8088/ws/cxf/findReportEntity?wsdl

这里写图片描述

能够访问,则启动成功。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lkmtao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值