Spring Boot - 如何从依赖包含REST端点?

Ole*_*hev 6 java spring spring-mvc

我是Spring/Spring Boot的新手,所以请原谅我所要求的是微不足道的.

我创建了Spring Boot应用程序,它暴露了REST端点:

package com.atomic.contentguard;

...
@Controller
@RequestMapping("/rest")
public class AcgController {

@RequestMapping(value="/acg-status",method=RequestMethod.GET)
@ResponseBody
 public String getStatus(){     
    return "Hi there!";
 }
}
Run Code Online (Sandbox Code Playgroud)

当您将其作为独立的Spring Boot应用程序运行时,一切正常,端点可以通过访问http:// localhost:8080/rest/acg-status来测试.

我想要实现的是"将它"带入另一个应用程序,该应用程序将我的应用程序作为pom.xml中的依赖项包括在内,期望这个REST端点显示在其中.

到目前为止我所做的是将它包含在另一个项目pom.xml中:

</dependencies>
    ...
    <dependency>
        <groupId>com.atomic</groupId>
        <artifactId>contentguard</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

然后将其包含在配置文件的其他应用程序@ComponentScan部分中:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.atomic.contentguard"})
public class EnvInfoWebConfig extends WebMvcConfigurerAdapter {

}
Run Code Online (Sandbox Code Playgroud)

但是,当您运行目标应用程序时,它不会显示:

No mapping found for HTTP request with URI [/other-application-context/rest/acg-status] in DispatcherServlet with name 'envinfo-dispatcher'
Run Code Online (Sandbox Code Playgroud)

我错过了什么/做错了什么?

dev*_*per 2

您可以通过在主项目中使用 spring boot Application Launcher 类来完成此操作,如下所示(您不需要 WebMvcConfigurerAdapter 类):

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = { "com.atomic.contentguard"})
public class AcgLauncher extends SpringBootServletInitializer {

    //This method is required to launch the ACG application
    public static void main(String[] args) {

        // Launch Trainserv Application
        SpringApplication.run(AcgLauncher.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

Spring Boot 在服务器启动期间使用该类,并扫描指定包中的所有 Spring 组件(控制器、服务、组件)。