java - 针对父类中定义的@RequestMapping使用多个子 Controller

标签 java spring spring-mvc controller abstract

我有一个父类 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/

相关文章:

java SSH 连接到 mysql 时出错,本地端口无法绑定(bind)

java - 为什么我必须在我的 Tiles 子页面片段中重新指定 taglib 注释? (已经在我的 masterlayout.jsp 中指定了)

java - 在 Spring : Caused by: org. hibernate.AnnotationException 中使用 Hibernate 的困难:针对未映射的类使用 @OneToMany 或 @ManyToMany:

java - 如何在 Spring Integration java dsl 中创建 header 值路由器并为其分配默认输出 channel ?

java - Sonar : You must define the following mandatory properties for unknown not resolved

java - 无法从 jar 导入类

java - Spring MVC,如何经常轮询服务器的最佳实践

java - Spring 和接口(interface)

java - 无法使用 poi mvc 在客户端生成下载 excel 文件

java - Papyrus (Eclipse) - 如何通过代码生成图表?