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!

相关推荐
辰海Coding11 小时前
MiniSpring框架学习-完成的 IoC 容器
java·spring boot·学习·架构
磊 子13 小时前
1.4CPU缓存一致性
java·spring cloud·缓存·系统
Maiko Star16 小时前
* SpringBoot整合LangChain4j
java·spring boot·后端·langchain4j
绝知此事17 小时前
【产品更名】通义灵码升级为 Qoder CN:AI 编码助手新时代,附大模型收费与 Spring Boot 支持全对比
人工智能·spring boot·后端·idea·ai编程
linmoo198617 小时前
Agent应用实践之四 - 基础:AgentScope-SpringBoot集成源码解析
人工智能·spring boot·agent·agentscope·openclaw
海兰18 小时前
【第21篇-续】graph-Stream-Node改造为适配openAI模型示例
java·人工智能·spring boot·spring·spring ai
Albert Edison18 小时前
基于 SpringBoot + RabbitMQ 完成企业级应用通信
spring boot·rabbitmq·java-rabbitmq
happymaker062620 小时前
Spring学习日记——DAY03(yml文件)
java·spring boot·spring
hikktn21 小时前
企业级Spring Boot应用管理:从零打造生产级启动脚本
java·spring boot·后端
霸道流氓气质21 小时前
Spring Boot + MyBatis-Plus 实现异常隔离的 Upsert 数据落库(含远程调用数据补全)
spring boot·后端·mybatis