0

I have the following classes:

abstract class BaseController { Handler handler; BaseController() { this.handler = handler; } @GetMapping("get") String get() { return handler.handle(); } } class SubControllerA extends BaseController { SubController() { super(SpecificHandlerA()); } } class SubControllerB extends BaseController { SubController() { super(SpecificHandlerB()); } } 

Is it possible to use the class-names of the implementing classes in the GetMapping, without overriding get(), so that I have two endpoints .../get/subcontrollera and .../get/subcontrollerb.

1 Answer 1

1

I'm not sure if my solution is the best. On the other hand, The @RequestMapping path should be set for SubControllers as Spring give Ambiguous Mapping error.

abstract class BaseController { Handler handler; BaseController(String handler, @PathVariable String pathName) { this.handler = handler; } @GetMapping("get/{pathName}") String get() { return handler.handle(); } } @RequestMapping("/a") class SubControllerA extends BaseController { private static final String pathName = SubControllerA.class.getSimpleName().toLowerCase(Locale.ROOT); SubController() { super(SpecificHandlerA(), pathName); } } @RequestMapping("/b") class SubControllerB extends BaseController { private static final String pathName = SubControllerB.class.getSimpleName().toLowerCase(Locale.ROOT); SubController() { super(SpecificHandlerB(), pathName); } } 

I hope, it is worked for you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.