SpringBoot如何给指定规则的控制器Controller请求添加前缀呢?
下文笔者讲述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请求前缀
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


