xtay2
xtay2

Reputation: 807

Override Methods with RequestMapping

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.

Upvotes: 0

Views: 162

Answers (1)

Veysel Öz
Veysel Öz

Reputation: 129

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.

Upvotes: 1

Related Questions