Spring-boot: Support default URI prefix for web service @RequestMapping

Created on 21 Jan 2016  路  5Comments  路  Source: spring-projects/spring-boot

There doesn't seem to be anyway, short of explicitly prefixing each individual @RequestMapping with a default URI like @RequestMapping(path="**/api**/controller1" at the top of the @RestController class, where **/api** is the default. I'm trying to have a clear separation in URL paths between normal application requests and web service requests, allowing me to provide additional security specific to web services.

I have an ongoing StackOverflow post, with no meaningful solution to-date and would like to suggest an enhancement to allow a default URI prefix to be supported somewhere in the Spring Boot application.properties configuration.

http://stackoverflow.com/questions/34801351/how-to-configure-a-default-restcontroller-uri-prefix-for-all-controllers

The result would be, using the example above, that my request mapping would be defined as:

@RequestMapping(path="controller1") 

The resulting URL would resolve to
> /<root_context>/api/controller1

Most helpful comment

Interesting. I wasn't aware you could do that. However, that still wouldn't be a great solution for me.
Since I can't have the @RequestMapping on the sub-class, I would have to prefix all method URI's to handle that. I was hoping for more of a 'building block' approach, like:

@RequestMapping(path="/api")
class BaseController{
    ....
}

@RequestMapping(path="sub-class1")
class SubClassedController1 extends BaseController{
   @RequestMapping(path="get-something") 
   public void getSomething(){
       ...
    }
}

@RequestMapping(path="sub-class2")
class SubClassedController2 extends BaseController{
   @RequestMapping(path="get-something") 
   public void getSomething(){
       ...
    }
}

Resulting in
/api/sub-class1/get-something
and
/api/sub-class2/get-something

That seems more natural to me.

All 5 comments

The approach that was suggested on the, now deleted, answer on Stack Overflow should work.

Annotate a base class at the class level with @RequestMapping("/api") and then subclass this controller. As long as you don't have a class-level @RequestMapping on the sub-class, any method-level @RequestMapping will be prefixed with /api.

Interesting. I wasn't aware you could do that. However, that still wouldn't be a great solution for me.
Since I can't have the @RequestMapping on the sub-class, I would have to prefix all method URI's to handle that. I was hoping for more of a 'building block' approach, like:

@RequestMapping(path="/api")
class BaseController{
    ....
}

@RequestMapping(path="sub-class1")
class SubClassedController1 extends BaseController{
   @RequestMapping(path="get-something") 
   public void getSomething(){
       ...
    }
}

@RequestMapping(path="sub-class2")
class SubClassedController2 extends BaseController{
   @RequestMapping(path="get-something") 
   public void getSomething(){
       ...
    }
}

Resulting in
/api/sub-class1/get-something
and
/api/sub-class2/get-something

That seems more natural to me.

@lewisdavidcole I can certainly see how the behaviour you have described could be useful. Unfortunately, it's pretty much out of Spring Boot's control as it's Spring MVC that defines this behavior and it isn't configurable. Please open a Spring MVC JIRA ticket (https://jira.spring.io/browse/SPR) if you'd like to see this enhancement.

use "spring.mvc.servlet.path: /api" instead

Was this page helpful?
0 / 5 - 0 ratings