How to configure a default @RestController URI prefix for all controllers?
<p>I know you can set the <code>server.contextPath</code> in <code>application.properties</code> to change the root context.</p>

<p>Also, I can add an additional context in the application config for Spring Boot like the following example (in Groovy) to add an "/api" to the URL mappings of the root context: </p>

<pre><code>@Bean
ServletRegistrationBean dispatcherServlet() {
     ServletRegistrationBean reg = new ServletRegistrationBean(new DispatcherServlet(), "/")
        reg.name = "dispatcherServlet"
        reg.addInitParameter("contextConfigLocation", "")
        reg.addUrlMappings("/api/*")
        reg.loadOnStartup = 2
        reg
    }
}
</code></pre>

<p>I am trying to have a separate base URI "/api" specifically for web service calls, that I can leverage for security, etc. However using the above approach will mean that any of my URIs, web service or not, can be reached with "/" or "/api", and provides no concrete segregation. </p>

<p>Is anyone aware of a better approach to set a base path for all <code>@RestController</code>(s) using configuration, without having to formally prefix every controller with /api/? If I am forced to manually prefix the URI for each controller, it would be possible to mistakenly omit that and bypass my security measures specific to web services.</p>

<p>Here is a reference in Stack Overflow to the same type of question, which was never completely answered:</p>

<p><a href="https://stackoverflow.com/questions/29439365/spring-boot-configure-a-url-prefix-for-restcontrollers">Spring Boot: Configure a url prefix for RestControllers</a></p>