相關歷史文章(閱讀本文之前,您可能需要先看下之前的系列👇)
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