如何将rest控制器添加到spring boot库,并在另一个应用程序中使用它?

Har*_*hit 7 spring-boot

我正在开发一个库xyz.jar,需要添加一个具有如下映射的 UI 页面:

@RestController
public class LibCtrl {
    
    @EventListener(ApplicationReadyEvent.class)
    @RequestMapping("/updateDomainList")
    String updateDomainList() {
        return "we can call a controller from another jar like this";
    }
}
Run Code Online (Sandbox Code Playgroud)

然后需要在我的主 springboot 应用程序中调用,myMainApplication.war所以当我调用

http://localhost/myMainApplication/updateDomainList

我应该看看

we can call controller from another jar like this
Run Code Online (Sandbox Code Playgroud)

在浏览器上。

如何才能实现这一点呢?@Component也不适合我。一旦开始起作用,是否@AutowiredJdbcTemplate起作用?

Har*_*hit 5

这是一个简单的修复。@ComponentScan 允许扫描多个包。这使得我可以添加由 Spring 管理的库包。只需将以下内容添加到您的应用程序类中即可。

@ComponentScan({"my.mainapplication.package","my.library.package"})
Run Code Online (Sandbox Code Playgroud)