Spring WebService的兩種主流實(shí)現(xiàn)方式?及說(shuō)明
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)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
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ù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
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開(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的使用及l(fā)ist,set和map三者的區(qū)別介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08

