简体   繁体   English

Spring Boot @RequestMapping继承

[英]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: 由于我需要提供两个API,因此在大多数情况下,这些控制器的控制器非常相似,因此我想使用抽象控制器和子控制器来实现类似以下的路由:

/sym/products / sym / products
/frontend/products / frontend / products

Where the "sym" or "frontend" part should be generated by abstract superclasses (controllers) like 应该由抽象超类(控制器)生成“ sym”或“ frontend”部分的位置

@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?) 顺便说一句:在父类中进行注释时,子类中的这些注释(@RestController和@CrossOrigin)是否仍然需要?

My problem: The routes are not being registered. 我的问题:路线未注册。 I only get a single route: /products 我只有一条路线:/ products

How to fix this? 如何解决这个问题?

I'd really appreciate your help! 非常感谢您的帮助! Thanks in advance, 提前致谢,

Tim 提姆

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. 控制器方法上的路径参数会附加到该类上定义的任何映射上(因此,例如,您可以根据需要将/products放在抽象方法上),但是您需要指定完整的基本路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM