核心提要:摒弃高风险的“推倒重来式大爆炸重构”,详解如何用 Nginx/网关绞杀者模式按模块将 Struts2 + JSP 存量业务逐一平滑迁移至 Spring Boot 3.4+,附完整 Action 改写前后代码对照。
一、为什么大型老系统不能“闭门重写一年再上线”?
十年历史的企业 Struts2 系统往往沉淀了成百上千条隐蔽业务规则。一次性推倒重写极易陷入“新系统追不上老系统需求变更”的泥潭。经过大量金融与政企项目验证的最佳路线是 绞杀者模式(Strangler Fig Pattern):
- 统一入口网关:在老系统前部署 Nginx 或 Spring Cloud Gateway,保持原有域名和 URL 路径不变。
- 共享登录态(Redis Session / JWT):先让老 Struts2 应用与新 Spring Boot 应用共享同一套登录态与数据库(初期共享库,后期逐步按领域拆库)。
- 按业务模块逐一替换:每完成一个模块(如客户查询、报表导出)的 Spring Boot 改造,仅在网关将该路径流量灰度切向新服务,出现异常可秒级切回老系统。
- 逐步下线老代码:直到所有
*.action路由清零,优雅下线老 Tomcat 集群。
二、代码重构实战:Struts2 Action → Spring Boot RestController
重构前(Struts2 多例有状态 Action):
public class CustomerAction extends ActionSupport {
private Long customerId; // 成员变量接收参数
private Customer customer; // 放入 ValueStack 供视图渲染
private CustomerService customerService;
public String detail() {
this.customer = customerService.findById(customerId);
return SUCCESS;
}
// 省略大量 getter / setter
}
重构后(Spring Boot 3.4+ 无状态单例 Controller + Java Record DTO):
@RestController
@RequestMapping("/api/v1/customers")
public class CustomerController {
private final CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
public record CustomerVO(Long id, String name, String level) {}
@GetMapping("/{id}")
public CustomerVO detail(@PathVariable Long id) {
Customer c = customerService.findById(id);
return new CustomerVO(c.getId(), c.getName(), c.getLevel());
}
}
效率提示:日常迁移查阅配置对照时,可配合使用本站的 Struts2 → Spring MVC 映射速查工具,快速核对拦截器、校验框架、国际化与异常处理的新旧等价写法。