Map controllers under a different context than actuator

227 views Asked by At

I would like to map all my controllers under a context path called /network, but my Actuator path under /actuator. I've attempted the following configuration in my application.properties, but the actuator path becomes /network/actuator, instead of /actuator:

server.servlet.context-path=/network
management.endpoints.web.base-path=/actuator

I've tried using the (deprecated) management.server.servlet.context-path property too, but this resulted in the same issue.

As a workaround, I can annotate my controllers with @RequestMapping, but it would be great if I can use the context path instead. I'm using Spring Boot 3.0.6.

2

There are 2 answers

1
Anton Tokmakov On

You can try to use such approach with separate configuration for this purpose:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class BasePathConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("network", HandlerTypePredicate.forBasePackage("com.example.controllers"));
    }
}
1
Scott Frederick On

This is the expected behavior. From the Spring Boot documentation:

Unless the management port has been configured to expose endpoints by using a different HTTP port, management.endpoints.web.base-path is relative to server.servlet.context-path (for servlet web applications) or spring.webflux.base-path (for reactive web applications). If management.server.port is configured, management.endpoints.web.base-path is relative to management.server.base-path.

So your options are to set management.server.port to a value that is different from server.port to get separate base paths, or change your application endpoint mappings as you suggested.