欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot如何根據(jù)目錄路徑生成接口的url路徑

 更新時(shí)間:2021年11月19日 10:52:43   作者:簡(jiǎn)單隨風(fēng)  
這篇文章主要介紹了SpringBoot如何根據(jù)目錄路徑生成接口的url路徑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

根據(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)文章

  • 讀取spring配置文件的方法(spring讀取資源文件)

    讀取spring配置文件的方法(spring讀取資源文件)

    這篇文章主要介紹了讀取spring配置文件的方法,需要的朋友可以參考下
    2014-02-02
  • SpringBoot中Tomcat和SpringMVC整合源碼分析

    SpringBoot中Tomcat和SpringMVC整合源碼分析

    Tomcat和SpringMVC都是通過這樣的方式進(jìn)行集成的,SpringBoot出現(xiàn)之前SpringMVC項(xiàng)目是直接部署在Tomcat服務(wù)器中的,這篇文章主要介紹了SpringBoot中Tomcat和SpringMVC整合源碼分析,需要的朋友可以參考下
    2022-07-07
  • Java算法之串的簡(jiǎn)單處理

    Java算法之串的簡(jiǎn)單處理

    今天小編就為大家分享一篇關(guān)于Java算法之串的簡(jiǎn)單處理,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • springboot解決Class path contains multiple SLF4J bindings問題

    springboot解決Class path contains multiple 

    這篇文章主要介紹了springboot解決Class path contains multiple SLF4J bindings問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Springboot過濾器禁止ip頻繁訪問功能實(shí)現(xiàn)

    Springboot過濾器禁止ip頻繁訪問功能實(shí)現(xiàn)

    這篇文章主要介紹了Springboot過濾器禁止ip頻繁訪問功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java增強(qiáng)for循環(huán)的增刪操作代碼

    Java增強(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-02
  • java如何保證多個(gè)線程按一定順序執(zhí)行

    java如何保證多個(gè)線程按一定順序執(zhí)行

    這篇文章主要介紹了java如何保證多個(gè)線程按一定順序執(zhí)行問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Idea設(shè)置spring boot應(yīng)用配置參數(shù)的兩種方式

    Idea設(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-11
  • springboot?vue接口測(cè)試前后端實(shí)現(xiàn)模塊樹列表功能

    springboot?vue接口測(cè)試前后端實(shí)現(xiàn)模塊樹列表功能

    這篇文章主要為大家介紹了springboot?vue接口測(cè)試前后端實(shí)現(xiàn)模塊樹列表功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Spring cloud 限流的多種方式

    Spring cloud 限流的多種方式

    在頻繁的網(wǎng)絡(luò)請(qǐng)求時(shí),服務(wù)有時(shí)候也會(huì)受到很大的壓力,尤其是那種網(wǎng)絡(luò)攻擊,非法的。這樣的情形有時(shí)候需要作一些限制。本文主要介紹了兩種限流方法,感興趣的可以了解一下
    2021-06-06

最新評(píng)論