Spring Boot REST调用从外部JAR在Controller上返回404

jon*_*zin 5 java rest spring jar spring-boot

我创建了一个外部JAR,其中包含一个简单的Spring REST控制器,其中包含以下代码:

@RestController
@RequestMapping("/hello")
public class HelloController {

  @RequestMapping(value = "/world")
  public Hello hello() {
    System.out.println("HELLO WORLD");
    return new Hello(1L, "Hello World");
  }

}
Run Code Online (Sandbox Code Playgroud)

然后,我将这个小项目编译到一个名为hello.jar的jar中,然后将其添加到Spring Boot应用程序的类路径中并启动该应用程序。

我还将该软件包添加到了ComponentScan中,如下所示:

@SpringBootApplication
@Configuration
// @Controller
// @org.springframework.context.annotation.Configuration
@ComponentScan(basePackages = {"main.java.foo.hello" })
@EnableEntityLinks
@EnableAutoConfiguration
public class Main {

  public static void main(final String[] args) throws Exception {

    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    URL[] urls = ((URLClassLoader)classLoader).getURLs();
    for (URL url : urls) {
      if (url.getPath().contains("hello")) {
        System.out.println(url.getPath());
      }
    }

    SpringApplication.run(Main.class);
  }
}
Run Code Online (Sandbox Code Playgroud)

由于打印出来,我可以看到jar已加载到应用程序中,并且通过将日志添加到Spring Boot Application中,我可以看到Controller已被扫描并被拾取(或至少看起来是被拾取)。

但是我去浏览(通过Chrome)或通过REST调用(通过Advance REST客户端)到“ localhost:8765 / hello / world ”(我在端口8765上启动服务器),却收到404错误。

其他的Rest Controller(从应用程序内部而不是外部JAR)似乎运行良好,因为所有REST调用均返回适当的结果。

有人认为他们知道为什么会返回404吗?