SpringBoot:控制器扩展了另一个但未检测到

时间:2019-02-04 15:34:20

标签: rest spring-boot controller

正如标题所述,该控制器是从另一个控制器继承而来的,具有一些其他控制器可以使用的常见功能。但是,应用程序无法检测到该控制器,并给出了404错误,而Swagger UI未显示该错误。

这是我在另一台PC上创建并使用git克隆的 maven项目。当我在那台电脑上运行它没有问题。在这台PC上,我试图删除继承的类,并且它起作用了,但我不会那样做,因为继承的控制器具有DTO的ModelMapper和其他控制器可以@RestController的功能使用。

ProfileController 没有显示在 profile 包中:

package com.hanabinoir.portfolio.profile;

public class ProfileController extends BaseController {

    //...
}

基础结构包中的继承的 BaseController

package com.hanabinoir.portfolio.infrastructure;

@RestController
@CrossOrigin
public class BaseController {

    protected ModelMapper modelMapper = new ModelMapper();
}

在从 BaseController 继承功能时,我需要 ProfileController 正确显示。如果我对项目结构有任何疑问,请给我一些建议。谢谢。

1 个答案:

答案 0 :(得分:1)

@Inherited中没有@RestController注释

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html

所以您的ProfileController没有附加注释

您可能需要定义一些自定义注释,并根据需要使用它

@Inherited
@RestController
public @interface InheritedRestController{
}

@InheritedRestController
public class BaseController {

}