You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单个Spring Boot应用如何为不同WebAPI配置独立路径前缀

实现方案

方案1:自定义注解标记Controller分组(推荐,无侵入性)

这种方式不需要修改现有Controller的@RequestMapping配置,仅通过标记注解区分不同业务模块的API分组即可。

第一步:定义分组标记注解

新增两个空的注解类,用来标记不同模块的Controller:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// 标记用户模块API
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface UserApi {
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// 标记星舰模块API
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface StarShipApi {
}

第二步:给对应Controller加注解

在原有的Controller上加上对应的注解即可,不用修改原有的路径配置:

@RestController
@RequestMapping("users")
@UserApi
class UserRestController  {
    // 接口方法...
}
@RestController
@RequestMapping("starships")
@StarShipApi
class StarShipRestController  {
    // 接口方法...
}

第三步:配置WebMvc读取前缀并绑定

在WebMvc配置类中读取application.properties的配置,给不同分组的Controller绑定对应的路径前缀:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.method.HandlerTypePredicate;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer  {
    // 读取配置中的前缀
    @Value("${api.user.pathPrefix}")
    private String userApiPrefix;
    @Value("${api.starship.pathPrefix}")
    private String starshipApiPrefix;

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 给用户模块API统一加前缀
        configurer.addPathPrefix(userApiPrefix, HandlerTypePredicate.forAnnotation(UserApi.class));
        // 给星舰模块API统一加前缀
        configurer.addPathPrefix(starshipApiPrefix, HandlerTypePredicate.forAnnotation(StarShipApi.class));
    }
}

方案2:按包名分组(无需新增注解)

如果不同模块的Controller已经分目录放在不同的包下,比如用户模块Controller在com.xxx.api.user包、星舰模块Controller在com.xxx.api.starship包,可以直接按包名匹配,不需要新增注解:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer  {
    @Value("${api.user.pathPrefix}")
    private String userApiPrefix;
    @Value("${api.starship.pathPrefix}")
    private String starshipApiPrefix;

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 给用户包下的所有Controller加前缀
        configurer.addPathPrefix(userApiPrefix, HandlerTypePredicate.forBasePackage("com.xxx.api.user"));
        // 给星舰包下的所有Controller加前缀
        configurer.addPathPrefix(starshipApiPrefix, HandlerTypePredicate.forBasePackage("com.xxx.api.starship"));
    }
}

最终效果

配置完成后,接口的实际访问路径为:

  • 用户模块接口:/api/v1/users/[具体接口路径]
  • 星舰模块接口:/universe/starships/[具体接口路径]

所有路径前缀完全由application.properties控制,不需要硬编码在Controller中。

内容的提问来源于stack exchange,提问作者Olaf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 12:54:03