我有一个父类 P,它定义了一个请求映射,如下所示:
public abstract class P {
@RequestMapping(value = "/a/b/c", method = RequestMethod.POST)
public String productLink(@RequestParam("abc") String json) throws Exception {
return getProductLinks(json);
}
}
我有几个子 Controller
类,ClassImpl
是其中之一:
@Controller
public class ClassImpl extends P {
@RequestMapping(value = "/start", method = RequestMethod.GET)
public String start(@RequestParam(value = "keyword", required = true) String keyword,
@RequestParam(value = "keywordId", required = true) long keywordId) throws Exception {
//Something
}
}
如果我仅使用一个子类运行此应用程序,它可以正常工作,但会导致多个子 Controller 出现问题。
当我运行我的应用程序时,出现错误“无法将处理程序 ClassImpl 映射到 URL 路径 [/a/b/c]:已经映射了处理程序 [a.b.c.d.ClassImpl@a92aaa]”
It seems that because of multiple child classes, it is unable to find the controller for this mapping which is understood.
在每个类(或一个单独的类)中定义@RequestMapping
是唯一的方法吗?我不想在所有地方都放置类似的代码。是否有任何解决方法可以将其保留在父类中并继续使用它?
谢谢
最佳答案
Is defining @RequestMapping in each class (or one separate class) the only way?
简短的回答是肯定的。我个人认为它属于一个单独的类别。
到底为什么要将 productLink()
放在父类中?它不是一个抽象方法,您也不会重写它,所以对我来说它没有多大意义。
关于java - 针对父类中定义的@RequestMapping使用多个子 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14607742/