Spring MVC Annotated Controller Interface with @PathVariable

后端 未结 5 1618
走了就别回头了
走了就别回头了 2020-11-30 02:47

Is there any reason not to map Controllers as interfaces?

In all the examples and questions I see surrounding controllers, all are concrete classes. Is there a reaso

5条回答
  •  北海茫月
    2020-11-30 03:42

    Apparently, when a request pattern is mapped to a method via the @RequestMapping annotation, it is mapped to to the concrete method implementation. So a request that matches the declaration will invoke GoalServiceImpl.removeGoal() directly rather than the method that originally declared the @RequestMapping ie GoalService.removeGoal().

    Since an annotation on an interface, interface method, or interface method parameter does not carry over to the implementation there is no way for Spring MVC to recognize this as a @PathVariable unless the implementing class declares it explicitly. Without it, any AOP advice that targets @PathVariable parameters will not be executed.

提交回复
热议问题