最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring WebService的兩種主流實(shí)現(xiàn)方式?及說(shuō)明

 更新時(shí)間:2026年04月27日 11:11:46   作者:曹牧  
文章介紹了Spring Web Services(Spring-WS)和Spring Boot + Apache CXF兩種主流的Web服務(wù)實(shí)現(xiàn)方式,Spring-WS采用ContractFirst方式,適合企業(yè)級(jí)、高可維護(hù)性的SOAP服務(wù);Spring Boot + CXF采用ContractLast方式,開(kāi)發(fā)更快,適合快速原型或內(nèi)部系統(tǒng)集成

Spring WebService主流實(shí)現(xiàn)方式‌

Spring-WS(Spring Web Services)‌:采用 ‌Contract First(自頂向下)‌ 方式,先定義 XSD/WSDL,再生成 Java 代碼。適用于企業(yè)級(jí)、高可維護(hù)性的 SOAP 服務(wù)。

Spring Boot + JAX-WS(通常用 Apache CXF)‌:采用 ‌Contract Last(自底向上)‌ 方式,通過(guò) @WebService 注解將 Java 類(lèi)暴露為 Web Service。開(kāi)發(fā)更快速,適合快速原型或內(nèi)部系統(tǒng)集成。

1、‌Spring-WS(Contract First)核心步驟‌

添加依賴(lài)‌(Maven):

? ? <dependency>
??????? <groupId>org.springframework.boot</groupId>
??????? <artifactId>spring-boot-starter-web-services</artifactId>
??? </dependency>
??? <dependency>
??????? <groupId>wsdl4j</groupId>
??????? <artifactId>wsdl4j</artifactId>
??? </dependency>

定義 XSD Schema‌(如 src/main/resources/xsd/login.xsd):

<xs:schema targetNamespace="http://example.com/ws/login"
?????????????? elementFormDefault="qualified"
?????????????? xmlns:xs="http://www.w3.org/2001/XMLSchema">
??????? <xs:element name="loginRequest">
??????????? <xs:complexType>
??????????????? <xs:sequence>
??????????????????? <xs:element name="username" type="xs:string"/>
??????????????????? <xs:element name="password" type="xs:string"/>
??????????????? </xs:sequence>
??????????? </xs:complexType>
??????? </xs:element>
??????? <xs:element name="loginResponse">
??????????? <xs:complexType>
??????????????? <xs:sequence>
??????????????????? <xs:element name="result" type="xs:string"/>
??????????????? </xs:sequence>
??????????? </xs:complexType>
??????? </xs:element>
??? </xs:schema>

自動(dòng)生成 Java 類(lèi)‌(使用 JAXB2 Maven 插件):

<plugin>
??????? <groupId>org.codehaus.mojo</groupId>
??????? <artifactId>jaxb2-maven-plugin</artifactId>
??????? <version>1.6</version>
??????? <configuration>
??????????? <schemaDirectory>${project.basedir}/src/main/resources/xsd/</schemaDirectory>
??????????? <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
??????? </configuration>
??????? <executions>
??????????? <execution>
??????????????? <goals><goal>xjc</goal></goals>
??????????? </execution>
??????? </executions>
??? </plugin>

創(chuàng)建 Endpoint‌:

@Endpoint
??? public class LoginEndpoint {
??????? private static final String NAMESPACE_URI = "http://example.com/ws/login";
??????? @PayloadRoot(namespace = NAMESPACE_URI, localPart = "loginRequest")
??????? @ResponsePayload
??????? public LoginResponse login(@RequestPayload LoginRequest request) {
??????????? // 業(yè)務(wù)邏輯
??????????? LoginResponse response = new LoginResponse();
??????????? response.setResult("success");
??????????? return response;
??????? }
??? }

配置 WebServiceConfig‌:

? ? @Configuration
??? @EnableWs
??? public class WebServiceConfig extends WsConfigurerAdapter {
??????? @Bean
??????? public ServletRegistrationBean messageDispatcherServlet(ApplicationContext context) {
??????????? MessageDispatcherServlet servlet = new MessageDispatcherServlet();
??????????? servlet.setApplicationContext(context);
??????????? servlet.setTransformWsdlLocations(true);
??????????? return new ServletRegistrationBean(servlet, "/ws/*");
??????? }
??????? @Bean(name = "login")
??????? public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) {
??????????? DefaultWsdl11Definition wsdl = new DefaultWsdl11Definition();
??????????? wsdl.setPortTypeName("loginPort");
??????????? wsdl.setLocationUri("/ws");
??????????? wsdl.setSchema(schema);
??????????? return wsdl;
??????? }
??????? @Bean
??????? public XsdSchema schema() {
??????????? return new SimpleXsdSchema(new ClassPathResource("xsd/login.xsd"));
??????? }
??? }

訪(fǎng)問(wèn) WSDL‌:

啟動(dòng)應(yīng)用后,訪(fǎng)問(wèn) http://localhost:8080/ws/login.wsdl 查看服務(wù)描述。

2、‌Spring Boot + CXF(Contract Last)

添加依賴(lài)‌:

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

定義接口與實(shí)現(xiàn)類(lèi)‌:

? ? @WebService
??? public interface OrderWS {
??????? @WebMethod
??????? Order getOrderById(int id);
??? }
??? @WebService(endpointInterface = "com.example.OrderWS")
??? public class OrderWSImpl implements OrderWS {
??????? @Override
??????? public Order getOrderById(int id) {
??????????? return new Order(id, "Product", 999.99);
??????? }
??? }

配置發(fā)布端點(diǎn)‌:

? ? @Configuration
??? public class WebServiceConfig {
??????? @Autowired
??????? private OrderWSImpl orderWSImpl;
??????? @Bean
??????? public Endpoint endpoint() {
??????????? EndpointImpl endpoint = new EndpointImpl(new SpringBus(), orderWSImpl);
??????????? endpoint.publish("/orderws");
??????????? return endpoint;
??????? }
??? }

訪(fǎng)問(wèn) WSDL‌:

http://localhost:8080/orderws?wsdl

3、‌注意事項(xiàng)‌

  • Spring-WS‌ 更適合需要嚴(yán)格契約控制、長(zhǎng)期維護(hù)的系統(tǒng)。
  • CXF + JAX-WS‌ 開(kāi)發(fā)更快,但耦合度較高,適合內(nèi)部服務(wù)或快速迭代場(chǎng)景。
  • 避免使用過(guò)時(shí)的 Axis1/Axis2,除非維護(hù)遺留系統(tǒng) ‌。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • MyBatis-Plus動(dòng)態(tài)返回實(shí)體類(lèi)示例詳解

    MyBatis-Plus動(dòng)態(tài)返回實(shí)體類(lèi)示例詳解

    這篇文章主要為大家介紹了MyBatis-Plus動(dòng)態(tài)返回實(shí)體類(lèi)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Android中的LinearLayout布局

    Android中的LinearLayout布局

    在一般情況下,當(dāng)有很多控件需要在一個(gè)界面列出來(lái)時(shí),我們就可以使用線(xiàn)性布局(LinearLayout)了,線(xiàn)性布局是按照垂直方向(vertical)或水平方向(horizontal)的順序依次排序子元素,每一個(gè)子元素都位于前一個(gè)元素之后,下面我們就簡(jiǎn)單的了解一下吧
    2017-01-01
  • java中g(shù)c算法實(shí)例用法

    java中g(shù)c算法實(shí)例用法

    在本篇文章里小編給大家整理了一篇關(guān)于java中g(shù)c算法實(shí)例用法,有興趣的朋友們可以參考學(xué)習(xí)下。
    2021-01-01
  • 在Spring中使用JDBC和JDBC模板的講解

    在Spring中使用JDBC和JDBC模板的講解

    今天小編就為大家分享一篇關(guān)于在Spring中使用JDBC和JDBC模板的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫(kù)分表的方案

    SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫(kù)分表的方案

    實(shí)現(xiàn)億級(jí)數(shù)據(jù)量分庫(kù)分表的項(xiàng)目是一個(gè)挑戰(zhàn)性很高的任務(wù),下面是一個(gè)基于Spring Boot的簡(jiǎn)單實(shí)現(xiàn)方案,感興趣的朋友一起看看吧
    2024-03-03
  • 使用springmvc的controller層獲取到請(qǐng)求的數(shù)據(jù)方式

    使用springmvc的controller層獲取到請(qǐng)求的數(shù)據(jù)方式

    這篇文章主要介紹了使用springmvc的controller層獲取到請(qǐng)求的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java實(shí)現(xiàn)最小高度樹(shù)

    Java實(shí)現(xiàn)最小高度樹(shù)

    本文主要介紹了Java實(shí)現(xiàn)最小高度樹(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • MVC+DAO設(shè)計(jì)模式下的設(shè)計(jì)流程詳解

    MVC+DAO設(shè)計(jì)模式下的設(shè)計(jì)流程詳解

    這篇文章主要介紹了MVC+DAO設(shè)計(jì)模式下的設(shè)計(jì)流程詳解,分別介紹了數(shù)據(jù)庫(kù)設(shè)計(jì)、設(shè)計(jì)符合java bean標(biāo)準(zhǔn)的entity類(lèi)、設(shè)計(jì)訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)的DAO接口等內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • SpringBoot接收前端參數(shù)的最常用的場(chǎng)景和具體案例

    SpringBoot接收前端參數(shù)的最常用的場(chǎng)景和具體案例

    在SpringBoot開(kāi)發(fā)中接收參數(shù)是非常常見(jiàn)且重要的一部分,依賴(lài)于請(qǐng)求的不同場(chǎng)景,Spring?Boot提供了多種方式來(lái)處理和接收參數(shù),項(xiàng)目小編就和大家簡(jiǎn)單講講SpringBoot接收前端參數(shù)的3 個(gè)最常用的場(chǎng)景和具體案例
    2025-11-11
  • 一段代碼搞懂關(guān)于Java中List、Set集合及Map的使用

    一段代碼搞懂關(guān)于Java中List、Set集合及Map的使用

    這篇文章主要介紹了關(guān)于Java中List、Set集合及Map的使用及l(fā)ist,set和map三者的區(qū)別介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08

最新評(píng)論

剑阁县| 东兴市| 兰州市| 丽水市| 湘潭县| 宜州市| 友谊县| 汕头市| 泽州县| 当阳市| 大荔县| 隆化县| 论坛| 濉溪县| 米泉市| 泌阳县| 十堰市| 海伦市| 东海县| 龙江县| 海晏县| 达尔| 隆子县| 太保市| 五指山市| 柳河县| 明溪县| 泽库县| 常州市| 融水| 新乡市| 扶沟县| 中超| 金堂县| 庆城县| 荆门市| 普陀区| 从化市| 无为县| 鄂托克旗| 锦州市|