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

Spring Boot如何为控制器设置独立根路径(不影响静态资源)

Solution for Adding a Shared Root URL to Controllers Without Affecting Static Files

Got it, let's break down how to solve this. You've already set server.servlet.contextPath which applies to both controllers and static files, but you want a separate prefix just for your controllers. Here are three practical approaches that get the job done without messing up your static content:

1. Use a Base Controller with Shared @RequestMapping

This is the simplest approach for most cases. Create a parent controller class with your desired prefix, then have all your other controllers extend it.

// Base controller to hold the shared prefix
@Controller
@RequestMapping("/api") // This is your controller-only root URL
public class BaseApiController {
    // No need for any methods here—just a placeholder for the prefix
}

// Example child controller
@Controller
public class UserController extends BaseApiController {
    @GetMapping("/users")
    public String listUsers() {
        // This endpoint will be accessible at /api/users
        return "user-list";
    }
}
  • Pros: Super straightforward, easy to implement and maintain.
  • Cons: Requires all your controllers to extend the base class. Works great if you can update existing controllers or start with this structure.

2. Configure Path Prefix via WebMvcConfigurer (Spring Boot 2.6+)

If you don't want to modify every controller, use Spring's built-in PathMatchConfigurer to add a prefix to all controller mappings automatically. This only targets classes annotated with @Controller or @RestController, so static files stay untouched.

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // Add "/api" prefix to all controllers
        configurer.addPathPrefix("/api", handlerMappingIntrospector -> 
            handlerMappingIntrospector.hasAnyAnnotation(Controller.class, RestController.class)
        );
    }
}
  • Pros: No changes needed to existing controllers. You can even tweak the filter to target specific controllers (e.g., only those with a custom annotation).
  • Cons: Requires Spring Boot 2.6 or newer. If you're on an older version, see the next approach.

3. Custom RequestMappingHandlerMapping (For Older Spring Boot Versions)

If you're stuck on Spring Boot <2.6, you can create a custom RequestMappingHandlerMapping to inject the prefix into all controller mappings.

@Configuration
public class CustomRequestMappingConfig {

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        return new RequestMappingHandlerMapping() {
            @Override
            protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
                RequestMappingInfo info = super.getMappingForMethod(method, handlerType);
                if (info != null) {
                    // Add your desired prefix here
                    info = RequestMappingInfo.paths("/api").build().combine(info);
                }
                return info;
            }
        };
    }
}
  • Pros: Works with older Spring Boot versions.
  • Cons: A bit more verbose, and you have to be careful not to override other default mappings.

Key Notes

All these approaches won't affect your static files because:

  • Static file paths are controlled by spring.mvc.static-path-pattern and spring.web.resources.static-locations, which are separate from controller request mappings.
  • The prefixes only apply to endpoints mapped via @Controller/@RestController, leaving your static content accessible at its original configured path.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:12:39