Spring WebService 的两种主流实现方式‌

  • ‌Spring-WS(Spring Web Services)‌:采用 ‌Contract First(自顶向下)‌ 方式,先定义 XSD/WSDL,再生成 Java 代码。适用于企业级、高可维护性的 SOAP 服务。
  • ‌Spring Boot + JAX-WS(通常用 Apache CXF)‌:采用 ‌Contract Last(自底向上)‌ 方式,通过 @WebService 注解将 Java 类暴露为 Web Service。开发更快速,适合快速原型或内部系统集成。

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

‌添加依赖‌(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>

‌自动生成 Java 类‌(使用 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>

‌创建 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) {

// 业务逻辑

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"));

}

}

‌访问 WSDL‌:

启动应用后,访问 http://localhost:8080/ws/login.wsdl 查看服务描述。

2、‌Spring Boot + CXF(Contract Last)

‌添加依赖‌:

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-spring-boot-starter-jaxws</artifactId>

<version>3.3.1</version>

</dependency>

‌定义接口与实现类‌:

@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);

}

}

‌配置发布端点‌:

@Configuration

public class WebServiceConfig {

@Autowired

private OrderWSImpl orderWSImpl;

@Bean

public Endpoint endpoint() {

EndpointImpl endpoint = new EndpointImpl(new SpringBus(), orderWSImpl);

endpoint.publish("/orderws");

return endpoint;

}

}

‌访问 WSDL‌:

http://localhost:8080/orderws?wsdl

3、‌注意事项‌

  • ‌Spring-WS‌ 更适合需要严格契约控制、长期维护的系统。
  • ‌CXF + JAX-WS‌ 开发更快,但耦合度较高,适合内部服务或快速迭代场景。
  • 避免使用过时的 Axis1/Axis2,除非维护遗留系统 ‌。
相关推荐
再写一行代码就下班3 分钟前
Cursor配置Java环境、创建Spring Boot项目的步骤
java·开发语言·spring boot
摇滚侠7 分钟前
Java 零基础全套教程,类的加载过程与类加载器的理解,笔记 189
java·后端·intellij-idea
ServBay14 分钟前
为什么我劝你不要在Mac上用Docker 进行本地 AI 开发
后端
蝎子莱莱爱打怪18 分钟前
XZLL-IM干货系列 02|Protobuf 协议设计:从 JSON 切到二进制,每条消息省了 60%
后端·面试·架构
程序员黑豆26 分钟前
AI全栈开发之Java:第一个Java程序
前端·后端·ai编程
kong@react28 分钟前
Rocky Linux 10.2 全面解析:企业级 CentOS 替代方案及保姆级docker安装
java·linux·运维·docker
小Q的编程笔记32 分钟前
Pump.fun 的核心是什么?用 300 行 Solidity 实现 Bonding Curve 与自动 LP 销毁
前端·后端·智能合约
学以智用34 分钟前
.NET Core Swagger 超详细讲解(从入门到企业级)
后端·.net
未若君雅裁34 分钟前
JVM 运行时数据区:程序计数器、堆、虚拟机栈与栈帧
java·jvm
摇滚侠35 分钟前
Spring 零基础入门到进阶 基于 XML 管理 Bean 14-28
xml·数据库·spring