最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot2為接口統(tǒng)一加url前綴的實(shí)現(xiàn)示例

 更新時間:2026年03月08日 09:19:56   作者:小恒恒  
本文主要介紹了SpringBoot2為接口統(tǒng)一加url前綴的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

背景

有時候會面臨前后端都放在一起的情況,這個時候我們就不好去使用配置中的 server.servlet.context-path ,經(jīng)過一番查找,發(fā)現(xiàn)使用注解還是挺方便的。

解決辦法

定義注解

package com.example.demo.annotation;

import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.lang.annotation.*;

/**
 * controller層統(tǒng)一使用該注解
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RestController
public @interface ApiRestController {
    /**
     * Alias for {@link Controller#value}.
     */
    @AliasFor(annotation = Controller.class)
    String value() default "";
}

實(shí)現(xiàn)WebMvcConfigurer配置路徑前綴

package com.example.demo.config;


import com.example.demo.annotation.ApiRestController;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置統(tǒng)一的后臺接口訪問路徑的前綴
 */
@Configuration
public class CustomWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer
                .addPathPrefix("/api", c -> c.isAnnotationPresent(ApiRestController.class));
    }

}

在Controller類上使用注解

package com.example.demo.controller.api;

import com.example.demo.annotation.ApiRestController;
import org.springframework.web.bind.annotation.GetMapping;

@ApiRestController
@RequestMapping("/token")
public class UserTokenController {

    /**
     * 獲取 user token
     */
    @GetMapping("/getUserToken")
    public ResponseBase getUserToken(HttpServletRequest request, HttpServletResponse response){
    }
}

到此這篇關(guān)于SpringBoot2為接口統(tǒng)一加url前綴的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot2 接口統(tǒng)一加url前綴內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

太原市| 上思县| 永泰县| 紫阳县| 镇沅| 林芝县| 中宁县| 临颍县| 兴安县| 米泉市| 蒙山县| 济阳县| 绥芬河市| 新余市| 都昌县| 宜兴市| 辽阳县| 观塘区| 洪雅县| 湘潭市| 高尔夫| 交城县| 渝北区| 浮山县| 永顺县| 中西区| 邵阳市| 河津市| 梅州市| 南华县| 康平县| 汕尾市| 富平县| 霍州市| 汽车| 牡丹江市| 高陵县| 迁西县| 东乡| 武宁县| 海淀区|