1. 程式人生 > >使用cxf在springMVC環境搭建webservice示例-服務端

使用cxf在springMVC環境搭建webservice示例-服務端

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

這裡寫圖片描述

能夠訪問,則啟動成功。