Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC (Spring Boot) - RequestMapping inheritance

Edit: please read the question curefully, I don't need answers that repeat what I wrote.

Looking aroung the web I found quite a confusion about this subject. What I'm looking for is a nice way to extend the value of a Controller's RequestMapping annotation.

Such as:

@Controller
@RequestMapping("/api")
public class ApiController {}

@Controller
@RequestMapping("/dashboard")
public class DashboardApiController extends ApiController {}

The result should be ("/api/dashboard").

This approach apparently simply override the RequestMapping value. A working approach may be to not put a RequestMapping annotation on the derived class.

@Controller
public class DashboardApiController extends ApiController
{
   @GetMapping("/dashboard")
   public String dashboardHome() {
      return "dashboard";
   }

   ... other methods prefixed with "/dashboard"
}

Is this the only feasible approach? I don't really like it.

like image 695
LppEdd Avatar asked Jun 30 '26 21:06

LppEdd


1 Answers

This is not the elegant solution you're looking for, but here's a functional solution I used.

@Controller
@RequestMapping(BASE_URI)
public class ApiController {
   protected final static String BASE_URI = "/api";
}

@Controller
@RequestMapping(ApiController.BASE_URI + "/dashboard")
public class DashboardApiController extends ApiController {}
like image 97
rdChris Avatar answered Jul 03 '26 18:07

rdChris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!