SpringBoot如何根據(jù)目錄路徑生成接口的url路徑
根據(jù)目錄路徑生成接口的url路徑
首先我們新建一個AutoPrefixUrlMapping類,繼承自RequestMappingHandlerMapping,用以獲取新的url
public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {
// 從配置文件中讀取根目錄
@Value("${api-package}")
private String apiPackagePath;
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo mappingInfo = super.getMappingForMethod(method, handlerType);
if (mappingInfo != null){
String prefix = this.getPrefix(handlerType);
RequestMappingInfo newMappingInfo = RequestMappingInfo.paths(prefix).build().combine(mappingInfo);
return newMappingInfo;
}
return mappingInfo;
}
// 獲取前綴
private String getPrefix(Class<?> handlerType){
String packageName = handlerType.getPackage().getName();
String newPath = packageName.replaceAll(this.apiPackagePath, "");
return newPath.replace(".","/");
}
}
配置文件application.proprties如下
api-package = com.lyn.miniprogram.api
用以聲明url的根目錄

然后我們創(chuàng)建一個AutoPrefixConfiguration類,用以將AutoPrefixUrlMapping加入Springboot的容器中
@Component
public class AutoPrefixConfiguration implements WebMvcRegistrations {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new AutoPrefixUrlMapping();
}
}
測試接口如下:

url會自動帶上 /v1的前綴,測試通過!當(dāng)然,如果目錄層級更復(fù)雜,通過上述代碼也是可以實(shí)現(xiàn)的。

springboot接口請求界面路徑返404
springboot正常啟動項(xiàng)目,訪問接口均正常,新增一接口請求界面路徑,訪問該接口報錯404
idea springboot
http://localhost:8080/cuer/apSw?ad=2893e6fce42&_=161607509 404
接口沒被掃描到
百度說是接口所在包放置位置不對,導(dǎo)致接口沒被掃描到,但是我的情況是系統(tǒng)原本存在的接口都可以訪問到,并且新接口所在位置與舊接口所在位置一致。且查看新接口包與 spring boot啟動類的位置符合可被掃描到情況,故排除新接口包放置位置不對的情況
配置或代碼寫法問題
查看后端接口代碼寫法沒錯
查看調(diào)用后端接口傳參是否錯誤(接口有參數(shù),直接不傳參http://localhost:8080/cuer/apSw,打斷點(diǎn) 訪問接口,可以進(jìn)入到接口,說明接口沒錯)斷點(diǎn)往下打,發(fā)現(xiàn)是return 頁面報404 ,比對后發(fā)現(xiàn),頁面路徑對應(yīng)根本沒頁面,修改頁面路徑重新訪問,成功。
@Slf4j
@Controller
@RequestMapping(value = {"/customer"})
@AllArgsConstructor
public class CerCustomerController {
private final PsionsUtil pnsUtil;
private final ICCredService crdService;
private final ICrEvalService ervice;
/**
* 跳轉(zhuǎn)到列表界面
*
* @return
*/
@GetMapping("/index")
public String customerCredIndex() {
return "/business/customer/index";
}
/**
* 跳轉(zhuǎn)到查看界面
*
* @return
*/
@GetMapping("/apeShw")
public String apseShw(String ad, Model model) {
model.addAttribute(“apId”, aId);
if (“CD”.equals(perUtil.getreTpe())) {
return “/bess/cuer/evfo”;
} else {
CeerVo ceo = creice.gtCByAlyId(ad);
model.addAttribute(“cd”, cVo);
return “/busis/cr/co”;
}
}
}
最后
找了挺久,有點(diǎn)坑哦,前端調(diào)用寫錯字母,就烏龍了
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于jpa中無法刪除onetomany中many問題的解決
這篇文章主要介紹了關(guān)于jpa中無法刪除onetomany中many問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java中實(shí)現(xiàn)分布式定時任務(wù)的方法
這篇文章主要介紹了Java中實(shí)現(xiàn)分布式定時任務(wù),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
利用Java實(shí)現(xiàn)解析網(wǎng)頁中的內(nèi)容
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言做一個解析指定網(wǎng)址的網(wǎng)頁內(nèi)容小應(yīng)用,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的可以嘗試下2022-10-10
線程池ThreadPoolExecutor并行處理實(shí)現(xiàn)代碼
這篇文章主要介紹了線程池ThreadPoolExecutor并行處理實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
關(guān)于Spring源碼深度解析(AOP功能源碼解析)
這篇文章主要介紹了關(guān)于Spring源碼深度解析(AOP功能源碼解析),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Java中報錯org.springframework.jdbc.UncategorizedSQLException的多種
本文主要介紹了Java中報錯org.springframework.jdbc.UncategorizedSQLException的多種解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

