SpringBoot如何给指定规则的控制器Controller请求添加前缀呢?

欣喜 SpringBoot 发布时间:2024-02-05 11:27:55 阅读数:7195 1
下文笔者讲述Springboot给指定规则的控制器请求添加前缀的方法及示例分享,如下所示
我们只需继承  WebMvcConfigurer接口
       重写 configurePathMatch 方法
       实现 指定controller规则添加
例:为指定Controller添加前缀的示例
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig implements WebMvcConfigurer {

    /**
     * 给后端所有请求增加后台请求前缀
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("api", s -> {
                    if (s.isAnnotationPresent(RequestMapping.class)) {
                        String url = s.getAnnotation(RequestMapping.class).value()[0];
                        boolean bool=url.startsWith("/system");
                        return bool;
                    }
                    return false;
                }
        );
    }
}
 
为请求url
    是/system开头
   统一添加一个 /api请求前缀
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringBoot/202402/7918.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者