SpringBoot根據(jù)目錄結(jié)構(gòu)自動(dòng)配置Url前綴方式
在很多其他框架中,比如Python的Flask、node.js的KOA,Controller要想能夠響應(yīng)前端的請(qǐng)求都需要我們主動(dòng)去注冊(cè)到應(yīng)用程序上。而Spring不需要我們自己去注冊(cè),由Spring通過掃描注解的方式去主動(dòng)發(fā)現(xiàn)。
自定義RequestMappingInfo
Spring中的RequestMappingHandlerMapping專門來負(fù)責(zé)處理標(biāo)注了@RequestMapping的控制器。創(chuàng)建一個(gè)類繼承并覆蓋其中的方法,從而實(shí)現(xiàn)對(duì)默認(rèn)機(jī)制的修改。
覆蓋其中的getMappingForMethod方法,這個(gè)方法的返回值RequestMappingInfo就包含了請(qǐng)求的Url,修改RequestMappingInfo中的Url從而修改路由中的Url。
package com.lin.missyou.hack;
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;
public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {
@Value("${missyou.api-package}")
private String apiPackagePath ; //從配置文件中獲取根包的路徑
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo requestMappingInfo = super.getMappingForMethod(method, handlerType);
if(null != requestMappingInfo){
//獲取url前綴
String prefix = getPrefix(handlerType);
//根據(jù)url前綴生成RequestMappingInfo并與原有的RequestMappingInfo合并
RequestMappingInfo mappingInfo = RequestMappingInfo.paths(prefix).build().combine(requestMappingInfo);
return mappingInfo;
}
return requestMappingInfo;
}
private String getPrefix(Class<?> handlerType){
String packageName = handlerType.getPackage().getName(); //獲取控制器所在包路徑
String dotPath = packageName.replaceAll(this.apiPackagePath,""); //將包路徑中多于的部分截取掉
return dotPath.replace(".","/"); //將包路徑中的.替換成/
}
}
通過接口的形式發(fā)現(xiàn)類
創(chuàng)建一個(gè)配置類AutoPrefixConfiguration將AutoPrefixUrlMapping加入到容器。配置類AutoPrefixConfiguration實(shí)現(xiàn)接口WebMvcRegistrations并覆蓋其中的getRequestMappingHandlerMapping方法
package com.lin.missyou.core.config;
import com.lin.missyou.hack.AutoPrefixUrlMapping;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Component
public class AutoPrefixConfiguration implements WebMvcRegistrations {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new AutoPrefixUrlMapping();
}
}
在配置文件中指定根包
missyou.api-package = com.lin.missyou.api
SprinBoot的 發(fā)現(xiàn)機(jī)制 有兩種。一種是在控制器上標(biāo)注特定注解,例如上一篇文章SpringBoot全局異常處理中在GlobalExceptionAdvice 上標(biāo)注@ControllerAdvice。另外一種是實(shí)現(xiàn)特定接口并覆蓋其中的特定方法,例如上面的AutoPrefixConfiguration。
測(cè)試一下

訪問結(jié)果,訪問路徑/v1/banner/test可以訪問到該控制器

將訪問路徑改為/banner/test就訪問不到了

將BannerController移動(dòng)到sample文件夾下訪問路徑/v1/sample/banner/test可以訪問到該控制器


這個(gè)方法存在一些爭(zhēng)議。一方面認(rèn)為根據(jù)目錄結(jié)構(gòu)自動(dòng)生成url雖然比較簡(jiǎn)單,少寫了一些代碼,但是無法通過控制器上標(biāo)注的@RequestMapping中的參數(shù)直接看出url,代碼的可讀性不是太好。
另一方面認(rèn)為,這個(gè)方法大大的簡(jiǎn)化了我們代碼的編寫,同時(shí)更加易于維護(hù),控制器隨意調(diào)整所在目錄都不需要去修改代碼。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項(xiàng)目使用slf4j的MDC日志打點(diǎn)功能(最新推薦)
這篇文章主要介紹了SpringBoot項(xiàng)目使用slf4j的MDC日志打點(diǎn)功能,本文通過示例代碼給大家介紹非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
MybatisPlus使用Mybatis的XML的動(dòng)態(tài)SQL的功能實(shí)現(xiàn)多表查詢
本文主要介紹了MybatisPlus使用Mybatis的XML的動(dòng)態(tài)SQL的功能實(shí)現(xiàn)多表查詢,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
基于Springboot+Netty實(shí)現(xiàn)rpc的方法 附demo
這篇文章主要介紹了基于Springboot+Netty實(shí)現(xiàn)rpc功能,在父項(xiàng)目中引入相關(guān)依賴結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
詳解Spring Security的formLogin登錄認(rèn)證模式
對(duì)于一個(gè)完整的應(yīng)用系統(tǒng),與登錄驗(yàn)證相關(guān)的頁面都是高度定制化的,非常美觀而且提供多種登錄方式。這就需要Spring Security支持我們自己定制登錄頁面,也就是本文給大家介紹的formLogin模式登錄認(rèn)證模式,感興趣的朋友跟隨小編一起看看吧2019-11-11
java?poi導(dǎo)入純數(shù)字等格式問題及解決
這篇文章主要介紹了java?poi導(dǎo)入純數(shù)字等格式問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot vue導(dǎo)出功能實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot vue導(dǎo)出功能實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
idea maven編譯報(bào)錯(cuò)Java heap space的解決方法
這篇文章主要為大家詳細(xì)介紹了idea maven編譯報(bào)錯(cuò)Java heap space的相關(guān)解決方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04

