How to configure a default @RestController URI prefix for all controllers? How to configure a default @RestController URI prefix for all controllers? spring spring

How to configure a default @RestController URI prefix for all controllers?


In continuation to the currently accepted solution the github issue addresses the same.

Spring 5.1 and above you can implement WebMvcConfigurer and override configurePathMatch method like below

@Configuration@EnableWebMvcpublic class WebConfig implements WebMvcConfigurer {    @Override    public void configurePathMatch(PathMatchConfigurer configurer) {        configurer.addPathPrefix("/api",                    HandlerTypePredicate.forAnnotation(RestController.class));    }}

Now all the @RestControllers will have /api as the prefix path alongside the path configured.

Official Documentation


There's a new solution to solve this kind of problem available since Spring Boot 1.4.0.RC1 (Details see https://github.com/spring-projects/spring-boot/issues/5004)

The solution of Shahin ASkari disables parts of the Auto configuration, so might cause other problems.

The following solution takes his idea and integrates it properly into spring boot. For my case I wanted all RestControllers with the base path api, but still serve static content with the root path (f.e. angular webapp)

Edit: I summed it up in a blog post with a slightly improved version see https://mhdevelopment.wordpress.com/2016/10/03/spring-restcontroller-specific-basepath/

@Configurationpublic class WebConfig {    @Bean    public WebMvcRegistrationsAdapter webMvcRegistrationsHandlerMapping() {        return new WebMvcRegistrationsAdapter() {            @Override            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {                return new RequestMappingHandlerMapping() {                    private final static String API_BASE_PATH = "api";                    @Override                    protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {                        Class<?> beanType = method.getDeclaringClass();                        RestController restApiController = beanType.getAnnotation(RestController.class);                        if (restApiController != null) {                            PatternsRequestCondition apiPattern = new PatternsRequestCondition(API_BASE_PATH)                                    .combine(mapping.getPatternsCondition());                            mapping = new RequestMappingInfo(mapping.getName(), apiPattern,                                    mapping.getMethodsCondition(), mapping.getParamsCondition(),                                    mapping.getHeadersCondition(), mapping.getConsumesCondition(),                                    mapping.getProducesCondition(), mapping.getCustomCondition());                        }                        super.registerHandlerMethod(handler, method, mapping);                    }                };            }        };    }}


Also You can achieve the same result by configuring WebMVC like this:

@Configurationpublic class PluginConfig implements WebMvcConfigurer {public static final String PREFIX = "/myprefix";@Overridepublic void configurePathMatch(PathMatchConfigurer configurer) {    configurer.addPathPrefix(PREFIX, c -> c.isAnnotationPresent(MyCustomAnnotation.class));}

}

  1. Implement WebMvcConfigurer on any @Configuration class.
  2. Override configurePathMatch method.
  3. You can do many useful things with PathMatchConfigurer e.g. add prefix for several classes, that satisfy predicate conditions.