SpringBoot如何根據(jù)目錄路徑生成接口的url路徑
根據(jù)目錄路徑生成接口的url路徑
首先我們新建一個(gè)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)建一個(gè)AutoPrefixConfiguration類,用以將AutoPrefixUrlMapping加入Springboot的容器中
@Component public class AutoPrefixConfiguration implements WebMvcRegistrations { @Override public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { return new AutoPrefixUrlMapping(); } }
測(cè)試接口如下:
url會(huì)自動(dòng)帶上 /v1的前綴,測(cè)試通過!當(dāng)然,如果目錄層級(jí)更復(fù)雜,通過上述代碼也是可以實(shí)現(xiàn)的。
springboot接口請(qǐng)求界面路徑返404
springboot正常啟動(dòng)項(xiàng)目,訪問接口均正常,新增一接口請(qǐng)求界面路徑,訪問該接口報(bào)錯(cuò)404
idea springboot
http://localhost:8080/cuer/apSw?ad=2893e6fce42&_=161607509 404
接口沒被掃描到
百度說是接口所在包放置位置不對(duì),導(dǎo)致接口沒被掃描到,但是我的情況是系統(tǒng)原本存在的接口都可以訪問到,并且新接口所在位置與舊接口所在位置一致。且查看新接口包與 spring boot啟動(dòng)類的位置符合可被掃描到情況,故排除新接口包放置位置不對(duì)的情況
配置或代碼寫法問題
查看后端接口代碼寫法沒錯(cuò)
查看調(diào)用后端接口傳參是否錯(cuò)誤(接口有參數(shù),直接不傳參http://localhost:8080/cuer/apSw,打斷點(diǎn) 訪問接口,可以進(jìn)入到接口,說明接口沒錯(cuò))斷點(diǎn)往下打,發(fā)現(xiàn)是return 頁面報(bào)404 ,比對(duì)后發(fā)現(xiàn),頁面路徑對(duì)應(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)用寫錯(cuò)字母,就烏龍了
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中Tomcat和SpringMVC整合源碼分析
Tomcat和SpringMVC都是通過這樣的方式進(jìn)行集成的,SpringBoot出現(xiàn)之前SpringMVC項(xiàng)目是直接部署在Tomcat服務(wù)器中的,這篇文章主要介紹了SpringBoot中Tomcat和SpringMVC整合源碼分析,需要的朋友可以參考下2022-07-07springboot解決Class path contains multiple 
這篇文章主要介紹了springboot解決Class path contains multiple SLF4J bindings問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Springboot過濾器禁止ip頻繁訪問功能實(shí)現(xiàn)
這篇文章主要介紹了Springboot過濾器禁止ip頻繁訪問功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java增強(qiáng)for循環(huán)的增刪操作代碼
Foreach循環(huán)(Foreach loop)是計(jì)算機(jī)編程語言中的一種控制流程語句,通常用來循環(huán)遍歷數(shù)組或集合中的元素,本文通過實(shí)例演示普通for循環(huán)和foreach循環(huán)使用,java增強(qiáng)for循環(huán)的操作代碼感興趣的朋友一起看看吧2024-02-02Idea設(shè)置spring boot應(yīng)用配置參數(shù)的兩種方式
本文通過兩個(gè)方式介紹Idea設(shè)置spring boot應(yīng)用配置參數(shù),一種是配置VM options的參數(shù)時(shí)要以:-DparamName的格式設(shè)置參數(shù),第二種可以參考下本文詳細(xì)設(shè)置,感興趣的朋友跟隨小編一起看看吧2023-11-11springboot?vue接口測(cè)試前后端實(shí)現(xiàn)模塊樹列表功能
這篇文章主要為大家介紹了springboot?vue接口測(cè)試前后端實(shí)現(xiàn)模塊樹列表功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05