Spring boot cxf構建webservice服務 - 第278篇

2021-12-29 SpringBoot


相關歷史文章(閱讀本文之前,您可能需要先看下之前的系列👇

WebService SOAP概述 - 第275篇

WSDL是什麼「Lese」 - 第276篇

Spring boot webservice怎麼玩? - 第277篇

說明

Spring Boot版本2.1.7

CXF版本3.3.3

 

一、前言

       在前面講了一堆理論的知識,最終還是要來實戰體驗下, 用身心感受下是一種什麼快感。

 

二、實現思路

       要使用CXF發布一個WebService具體都是需要做什麼吶?

(1)CXF依賴包:添加CXF的依賴包,主要是是cxf-spring-boot-starter-jaxws。

(2)創建WebService以及實現:使用@WebService構建WebService以及對其進行實現。

(3)發布WebService:添加配置類發布WebService。

 

三、具體實現

       根據以上的思路就可以實際來操作下了:

3.1 創建項目

       創建項目:spring-boot-cxf

3.2 添加依賴

       添加依賴,主要是SpringBoot和cxf:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath /> 
    </parent>
    <groupId>com.kfit</groupId>
    <artifactId>spring-boot-cxf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-cxf</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.3</version>
        </dependency>

    </dependencies>
</project>

 

3.3 創建web service服務

       使用@WebService創建一個接口HelloService:

package com.kfit.demo.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;


@WebService(targetNamespace = "http://ws.demo.kfit.com") // 命名空間,一般是接口的包名倒序
public interface HelloService {
    @WebMethod
    String sayHello(@WebParam(name = "userName") String name);
}

       實現HelloService:

package com.kfit.demo.ws.impl;

import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.kfit.demo.ws.HelloService;


@WebService(serviceName = "helloService"// 服務名
        , targetNamespace = "http://ws.demo.kfit.com"// 包名倒序,並且和接口定義保持一致
        , endpointInterface = "com.kfit.demo.ws.HelloService") // 包名
@Component
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "hello , " + name;
    }

}

 

3.4 發布web service服務

       編寫好web service服務之後,需要將服務發布到指定的路徑下:

package com.kfit.demo.config;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.kfit.demo.ws.HelloService;


@Configuration
public class CxfConfig {

    @Autowired
    private HelloService helloService;

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(),helloService);
        endpoint.publish("/helloService");
        return endpoint;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Bean("cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

}

說明:dispatcherServlet()需要重命名,否則原本的mvc就被覆蓋了。

 

3.6 測試

       啟動項目,訪問測試下吧:http://127.0.0.1:8080/cxf:

訪問可以看到一條記錄,點擊連結查看WSDL文檔:http://127.0.0.1:8080/cxf/helloService?wsdl

<wsdl:definitions
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns="http://ws.demo.kfit.com"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="helloService"
    targetNamespace="http://ws.demo.kfit.com">
    <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://ws.demo.kfit.com" elementFormDefault="unqualified"
            targetNamespace="http://ws.demo.kfit.com" version="1.0">
            <xs:element name="sayHello" type="tns:sayHello" />
            <xs:element name="sayHelloResponse"
                type="tns:sayHelloResponse" />
            <xs:complexType name="sayHello">
                <xs:sequence>
                    <xs:element minOccurs="0" name="userName"
                        type="xs:string" />
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="sayHelloResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" type="xs:string" />
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="sayHelloResponse">
        <wsdl:part element="tns:sayHelloResponse" name="parameters">
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="sayHello">
        <wsdl:part element="tns:sayHello" name="parameters">
        </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="HelloService">
        <wsdl:operation name="sayHello">
            <wsdl:input message="tns:sayHello" name="sayHello">
            </wsdl:input>
            <wsdl:output message="tns:sayHelloResponse"
                name="sayHelloResponse">
            </wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="helloServiceSoapBinding"
        type="tns:HelloService">
        <soap:binding style="document"
            transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="sayHello">
            <soap:operation soapAction="" style="document" />
            <wsdl:input name="sayHello">
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output name="sayHelloResponse">
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="helloService">
        <wsdl:port binding="tns:helloServiceSoapBinding"
            name="HelloServiceImplPort">
            <soap:address
                location="http://127.0.0.1:8080/cxf/helloService" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

       到此我們就已經發布了一個簡單的Hello CXF。

       發布了一個WebService總要知道怎麼調用吧,下文進行分析如何調用上面的Hello cxf以及調用查詢手機號歸屬地。

我就是我,是顏色不一樣的煙火。
我就是我,是與眾不同的小蘋果。

à悟空學院:http://t.cn/Rg3fKJD

學院中有Spring Boot相關的課程!點擊「閱讀原文」進行查看!

SpringBoot視頻:http://t.cn/R3QepWG

Spring Cloud視頻:http://t.cn/R3QeRZc

SpringBoot Shiro視頻:http://t.cn/R3QDMbh

SpringBoot交流平臺:http://t.cn/R3QDhU0

SpringData和JPA視頻:http://t.cn/R1pSojf

SpringSecurity5.0視頻:http://t.cn/EwlLjHh

Sharding-JDBC分庫分表實戰:http://t.cn/E4lpD6e

相關焦點

  • Spring boot cxf調用webservice服務 - 第279篇
    篇WSDL是什麼「Lese」 - 第276篇Spring boot webservice怎麼玩?- 第277篇Spring boot cxf構建webservice服務 - 第278篇一、前言       在前面的小節中已經成功發布了web service服務,這一節看看怎麼調用。
  • WebService就是這麼簡單
    的服務,其實這兩種方式也有弊端如果我們可以把整個對象傳遞進去,返回的結果更加友好的話,就好像我們平常調用Java類一樣使用webservice就好咯!Java也提供了類似的方法,把webservice服務搞成是Java類讓我們自己調用,既然是Java類的話,那麼我們使用起來就非常方便了!
  • WebService接口調用實戰之Apache CXF
    本次技術:        springboot        cxf. 等一、下載Apache CXF並配置環境變量下載地址:http://cxf.apache.org/download.html下載並解壓到硬碟!
  • Spring Boot中使用Mockito進行Web測試 - 第339篇
    篇SpringBoot框架開發的優秀的項目「值得收藏學習」 - 第335從Spring整合第三方框架學習Spring Boot - 第336篇Mock工具之Mockito - 第337篇Spring Boot中使用Mockito - 第338篇悟纖:師傅,我上面mock的service層和dao層的例子?
  • Spring Boot | 一個90後女孩的第一個 Spring Boot 應用
    我是小小,今天是本周的第五篇,本周第五篇將會著重講解 Spring Boot 之 Hello World前言Spring Boot 出現的原因Spring Boot的出現,主要是用來解決 Spring 過去的一些問題,提出了約定優於配置的思想,默認對很多方法進行了設置,使得開發者可以快速的構建項目,集成第三方的內容。使得開發效率大大提升。
  • Spring Boot 2.0(四):使用 Docker 部署 Spring Boot
    Docker 技術發展為微服務落地提供了更加便利的環境,使用 Docker 部署 Spring Boot 其實非常簡單,這篇文章我們就來簡單學習下。
  • Spring Boot 分層構建 Docker 鏡像實戰
    包就可以構建基於分層模式的 Docker 鏡像:項目 pom.xml 中引入 SpringBoot 2.3.x 依賴:<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent
  • Spring Boot 接入 GitHub 第三方登錄
    > 連結:zyc.red/Spring/Security/OAuth2/OAuth2-Client/前言OAuth(開放授權)是一個開放標準,允許用戶授權第三方網站訪問他們存儲在另外的服務提供者上的信息
  • 使用 Docker 部署 Spring Boot 項目
    Docker 技術發展為微服務落地提供了更加便利的環境,使用 Docker 部署 Spring Boot 其實非常簡單,這篇文章我們就來簡單學習下。首先構建一個簡單的 Spring Boot 項目,然後給項目添加 Docker 支持,最後對項目進行部署。
  • Spring Boot WebFlux Quick Start
    註解的會在第二篇文章講到,下面快速入門用 Spring Webflux 功能性方式實現。例如,添加 spring-boot-starter-webflux 依賴,就可用於構建響應式 API 服務,其包含了 Web Flux 和 Tomcat 內嵌容器等。開發中,很多功能是通過添加 Starter 組件的方式來進行實現。那麼,Spring Boot 2.x 常用的 Starter 組件有哪些呢?
  • 架構實戰篇(六):Spring Boot RestTemplate的使用
    當然有了那就是RestTemplate一、什麼是RestTemplate傳統情況下在java代碼裡訪問restful服務,一般使用Apache的HttpClient。不過此種方法使用起來太過繁瑣。spring提供了一種簡單便捷的模板類來進行操作,這就是RestTemplate。
  • Spring Cloud第一篇:服務註冊與發現Eureka
    一、spring cloud簡介spring cloud 為開發人員提供了快速構建分布式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理
  • 實戰篇:5分鐘實現SpringBoot整合Dubbo構建分布式服務
    ,從註冊中心中訂閱服務Monitor:監控中心,RPC調用次數和調用時間監控從上圖中我們可以了解到整個RPC 服務調用的過程主要為:生產者發布服務到服務註冊中心中消費者在服務註冊中心中訂閱服務消費者調用已經註冊的服務一、項目構建
  • Spring Boot中增強對MongoDB的配置(連接池等)
    之前在博客上轉載了一篇關於《如何在Spring Boot中是配置MongoDB的連接數》的文章,相信關注我博客的朋友們肯定也看過了。
  • Spring Boot 2.x基礎教程:快速入門
    簡介在您第1次接觸和學習Spring框架的時候,是否因為其繁雜的配置而退卻了?在你第n次使用Spring框架的時候,是否覺得一堆反覆黏貼的配置有一些厭煩?那麼您就不妨來試試使用Spring Boot來讓你更易上手,更簡單快捷地構建Spring應用!Spring Boot讓我們的Spring應用變的更輕量化。
  • WebService SOAP概述 - 第275篇
    一、何為WebServiceWebService技術,能使得運行在不同機器上的不同應用無須藉助附加的、專門的第三方軟體或硬體, 就可相互交換數據或集成。依據Web Service規範實施的應用之間,無論它們所使用的語言、平臺或內部協議是什麼,都可以相互交換數據。
  • Spring Boot實戰:Restful API的構建
    跟Spring boot有什麼關係?其實Spring boot的作用就是為我們省去了配置的過程,其他功能確實都是Spring與Spring MVC來為我們提供的,大家應該記得Spring boot通過各種starter來為我們提供自動配置的服務,我們的工程裡面之前引入過這個依賴:<dependency>      <groupId>org.springframework.boot
  • 用Spring Boot打包你的React應用
    先講一講這篇文章的背景故事。之前我的團隊需要在我們需求的基礎架構上節省一些資金,並且由於我們要構建的這個應用程式中,大部分負載都會在客戶端而非服務端上,所以我們決定試驗一下能否將一個 Spring 應用程式與一個 React 應用結合起來,並打包成一個 war 文件。
  • Spring Boot 2.x基礎教程:使用Swagger2構建強大的API文檔
    又碰到了運營號直接拿了文章就推,導致沒法標原創。隨著前後端分離架構和微服務架構的流行,我們使用Spring Boot來構建RESTful API項目的場景越來越多。通常我們的一個RESTful API就有可能要服務於多個不同的開發人員或開發團隊:IOS開發、Android開發、Web開發甚至其他的後端服務等。
  • Spring Boot Web 開發註解篇
    內容包括 MVC 模式的實現和 RESTful 服務的支持。2.1 Spring MVC 體系溫故知新spring-webmvc 模塊裡面包:- org.springframework.web.servlet 提供與應用程式上下文基礎結構集成的 Servlet,以及 Spring web MVC 框架的核心接口和類。