springMvc如何动态替换请求路径?

需求背景:

假设你的系统预先开发了很多接口,对外提供的接口风格是统一的,约定在接口地址中嵌入某些参数,比如:user/{token}/getUserInfo ,该系统一直以这样的方式提供给第三方,突然有一天来了一个非常重要的大客户,人家要求接口地址不能发生任何改变,否则他们网关无法过滤请求,比如上述接口应该改成:user/xxx/getUserInfo,其他信息如:token改在请求头中传,这时你该怎么做?

有人会说:这还怎么做啊?不就是手动改一下之前的接口么?

但是你改了之前的接口的话,那你已经对接了的第三方该怎么办?所以改老接口的方式肯定是行不通的了。当然我们也可以把之前的接口再写一遍,但是这样的我们会有大量的重复代码。

所以在这里给大家提供一种基于拦截器的思路实现:

java 复制代码
public class TokenInterceptor implements HandlerInterceptor{
        private static final String PathVariableKey = "org.springframework.web.servlet.HandlerMapping.uriTemplateVariables";

        private static final String PathVariableValue = "token";
        
        
        private String authUrl="/user/xxx/getUserInfo";
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            if (request == null || request.getRequestURI() == null || !StringUtils.contains(request.getRequestURI().toLowerCase(), authUrl)) {
            return true;
        }
       
        LinkedHashMap<String, Object> attribute = (LinkedHashMap<String, Object>) request.getAttribute(PathVariableKey);
        if (attribute == null) {
            return true;
        }
        attribute.entrySet().forEach(entry -> {
            if (Objects.equals(PathVariableValue, entry.getKey())) {
                entry.setValue(appKey);
            }
        });
        request.setAttribute(PathVariableKey, attribute);
        return true;
    }
}

这样我们就能实现前端请求的接口地址是:user/xxx/getUserInfo 而到了后台以后自动映射成user/{token}/getUserInfo!

相关推荐
养军博客34 分钟前
spring boot3.0自定义校验注解:文章状态校验示例
java·前端·spring boot
小赵面校招1 小时前
Spring Boot整合MyBatis全攻略:原理剖析与最佳实践
java·spring boot·mybatis
曼岛_1 小时前
[Java实战]Spring Boot 3 整合 Ehcache 3(十九)
java·spring boot·spring
意倾城1 小时前
Spring Boot 配置文件敏感信息加密:Jasypt 实战
java·spring boot·后端
曼岛_1 小时前
[Java实战]Spring Boot 3 整合 Apache Shiro(二十一)
java·spring boot·apache
火皇4051 小时前
Spring Boot 使用 OSHI 实现系统运行状态监控接口
java·spring boot·后端
小赵面校招1 小时前
SpringBoot整合MyBatis-Plus:零XML实现高效CRUD
xml·spring boot·mybatis
不再幻想,脚踏实地1 小时前
Spring Boot配置文件
java·数据库·spring boot
pedestrian_h1 小时前
Spring AI 开发本地deepseek对话快速上手笔记
java·spring boot·笔记·llm·ollama·deepseek
SuperherRo1 小时前
Web开发-JavaEE应用&SpringBoot栈&Actuator&Swagger&HeapDump&提取自动化
spring boot·java-ee·actuator