SpringBoot根據(jù)目錄結(jié)構(gòu)自動生成路由前綴的實現(xiàn)代碼
前言
本文介紹如何根據(jù)目錄結(jié)構(gòu)給RequestMapping添加路由前綴(覆蓋RequestMappingHandlerMapping中的getMappingForMethod方法,修改其中的Url),如下圖的實際訪問路徑為:/v1/test/test。

具體實現(xiàn)
配置文件指定基礎包
application.properties
api-package = com.coisini.springbootlearn.controller
自動補全路由前綴處理類
AutoPrefixUrlMapping.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method;
import java.util.Objects;
/**
* @Description 自動補全路由前綴處理類
* RequestMappingHandlerMapping 負責處理標注了@RequestMapping的控制器
* @author coisini
* @date Aug 10, 2021
* @Version 1.0
*/
public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {
/**
* 讀取基礎包配置
*/
@Value("${api-package}")
private String bathApiPackagePath;
/**
* 重寫方法路由獲取
* @param method
* @param handlerType
* @return
*/
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo mappingInfo = super.getMappingForMethod(method, handlerType);
if (Objects.nonNull(mappingInfo)) {
String prefix = this.getPrefix(handlerType);
/**
* RequestMappingInfo.paths(prefix).build() 根據(jù)前綴生成mappingInfo
* combine(mappingInfo) 拼接原來的mappingInfo
*/
return RequestMappingInfo.paths(prefix).build().combine(mappingInfo);
}
return mappingInfo;
}
/**
* 獲取方法路由前綴
* @param handleType
* @return
*/
private String getPrefix(Class<?> handleType) {
String packageName = handleType.getPackage().getName();
String dotPath = packageName.replace(this.bathApiPackagePath, "").replace(".","/");
return dotPath;
}
}
自動補全路由前綴配置類
AutoPrefixConfiguration.java
/**
* @Description 自動補全路由前綴配置類
* 通過接口的形式主動發(fā)現(xiàn)
* @author coisini
* @date Aug 10, 2021
* @Version 1.0
*/
@Component
public class AutoPrefixConfiguration implements WebMvcRegistrations {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new AutoPrefixUrlMapping();
}
}
測試類
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping(value = "/test")
public String test(){
return "hello";
}
}
測試
目錄結(jié)構(gòu)如下

訪問結(jié)果

目錄結(jié)構(gòu)變更

訪問結(jié)果

到此這篇關(guān)于SpringBoot - 根據(jù)目錄結(jié)構(gòu)自動生成路由前綴的文章就介紹到這了,更多相關(guān)SpringBoot目錄結(jié)構(gòu)路由前綴內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot如何使用MyBatisPlus逆向工程自動生成代碼
- SpringBoot集成Mybatis-plus并實現(xiàn)自動生成相關(guān)文件的示例代碼
- Springboot Mybatis Plus自動生成工具類詳解代碼
- SpringBoot項目使用mybatis-plus逆向自動生成全套代碼
- SpringBoot整合Mybatis Generator自動生成代碼
- springboot整合freemarker代碼自動生成器
- SpringBoot整合screw實現(xiàn)數(shù)據(jù)庫文檔自動生成的示例代碼
- springboot 通過代碼自動生成pid的方法
- SpringBoot+MyBatis-Plus+Velocity實現(xiàn)代碼自動生成
相關(guān)文章
SpringBoot項目中獲取IP地址的實現(xiàn)示例
OkHttp是一個由Square開發(fā)的高效、現(xiàn)代的HTTP客戶端庫,本文主要介紹了SpringBoot項目中獲取IP地址的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-08-08
關(guān)于Spring中@Value注解使用和源碼分析
通過深入分析@Value注解的使用和源碼,本文詳細解釋了Spring如何解析@Value注解并為屬性賦值,首先,Spring會解析并收集所有被@Value注解修飾的屬性,這一過程依賴于AutowiredAnnotationBeanPostProcessor類2024-11-11
JAVA JSP頁面技術(shù)之EL表達式整理歸納總結(jié)
這篇文章主要介紹了java中JSP頁面技術(shù)之EL表達式概念作用以及語法等的使用,需要的朋友可以參考2017-04-04
Java中將異步調(diào)用轉(zhuǎn)為同步的五種實現(xiàn)方法
本文介紹了將異步調(diào)用轉(zhuǎn)為同步阻塞模式的五種方法:wait/notify、ReentrantLock+Condition、Future、CountDownLatch和CyclicBarrier,每種方法都有其適用場景和核心機制,可以根據(jù)具體需求選擇合適的方法,需要的朋友可以參考下2025-02-02
Spring注解驅(qū)動之@EventListener注解使用方式
這篇文章主要介紹了Spring注解驅(qū)動之@EventListener注解使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
SpringMVC中使用@PathVariable綁定路由中的數(shù)組的方法
這篇文章主要介紹了SpringMVC中使用@PathVariable綁定路由中的數(shù)組的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Jvisualvm監(jiān)控遠程SpringBoot項目的過程詳解
這篇文章主要介紹了Jvisualvm監(jiān)控遠程SpringBoot項目,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04

