Search code examples
javaspringrestinheritancerequest-mapping

Spring Boot @RequestMapping Inheritance


since I need to serve two APIs which controllers are very similar in most of all cases and therefore I'd like to work with abstract controllers and sub controllers in order to achieve routes like:

/sym/products
/frontend/products

Where the "sym" or "frontend" part should be generated by abstract superclasses (controllers) like

@RestController
@CrossOrigin
@RequestMapping("sym")
public abstract class SymphonyBaseController {}

and

@RestController
@CrossOrigin
@RequestMapping("frontend")
public abstract class FrontendBaseController {}

so I could do:

@RestController
@CrossOrigin
@RequestMapping("products")
public class ProductController extends SymphonyBaseController {}

Btw: are these annotations (@RestController and @CrossOrigin) in the subclasses necessary anyway when annotated in super class already?)

My problem: The routes are not being registered. I only get a single route: /products

How to fix this?

I'd really appreciate your help! Thanks in advance,

Tim


Solution

  • Values for the exact same parameter override on subclasses, they don't accumulate. Path parameters on controller methods are appended to any mapping defined on the class (and so you could, for example, put /products on an abstract method if you like), but you'll need to specify the full base path.