SpringBoot 无法扫描 Controller 并获取 URL 映射

SpringBoot can't scan Controller and get the URL mapping

我刚刚编写了一个 Springboot 应用程序作为测试 Web 服务器,它源自 spring.io。 它最初打包在 "jar" 中,将其更改为 "war" 文件。 并获取应用程序 class 代码如下:

package com.ronzhong;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.context.WebApplicationContext;

@SpringBootApplication(scanBasePackages={"com.ronzhong.libraryManagement"})
public class LibraryManagementApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        application.application().setBannerMode(Mode.OFF);
        return application.sources(SpringApplicationBuilder.class);
    }

    protected WebApplicationContext run(SpringApplication application) {
        return (WebApplicationContext) application.run();
    }

//  public static void main(String[] args) {
//  SpringApplication.run(LibraryManagementApplication.class, args);
//}
}

控制器是:

package com.ronzhong.libraryManagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.*;

//@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@RestController
@RequestMapping("/api")
public class CounterController {
    @Autowired
    private BookService bookService;

    @PostMapping
    public boolean add(@RequestBody Book book){
        return bookService.add(book);
    }

    @RequestMapping(value = "/books", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Book[] get(){
        return bookService.getBooks();
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Book findOne(@PathVariable("id") int id){
        return bookService.findById(id);
    }

}

而且我还检查了 "Dynamic Web Module" 在项目方面。 最后,我在启动 tomcat 服务器时得到了这些日志,并生成了这个 war 文件。

INFO: 2 Spring WebApplicationInitializers detected on classpath
2018-11-28 10:43:31.925  INFO 14888 --- [           main] c.ronzhong.LibraryManagementApplication  : Starting LibraryManagementApplication on ronzhongmachine with PID 14888 (C:\Users\ronzhong\workspace_testserver\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\library-Management\WEB-INF\classes started by ronzhong in C:\tools_installers\eclipse-jee-2018-09-win32-x86_64\eclipse)
2018-11-28 10:43:32.002  INFO 14888 --- [           main] c.ronzhong.LibraryManagementApplication  : No active profile set, falling back to default profiles: default
2018-11-28 10:43:33.910  INFO 14888 --- [           main] o.a.c.c.C.[.[.[/library-Management]      : Initializing Spring embedded WebApplicationContext
2018-11-28 10:43:33.911  INFO 14888 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1604 ms
2018-11-28 10:43:34.362  INFO 14888 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'errorPageFilter' to: [/*]
2018-11-28 10:43:34.704  INFO 14888 --- [           main] c.ronzhong.LibraryManagementApplication  : Started LibraryManagementApplication in 5.958 seconds (JVM running for 36.407)
2018-11-28 10:43:34.955  INFO 14888 --- [           main] org.apache.coyote.ajp.AjpNioProtocol     : Starting ProtocolHandler ["ajp-nio-8009"]
2018-11-28 10:43:34.968  INFO 14888 --- [           main] org.apache.catalina.startup.Catalina     : Server startup in 28177 ms

日志中没有映射信息。 如果我用 http://localhost:8080/api/books 请求,它会得到 404 错误。 但是日志一点也没有改变。 所以我用谷歌搜索了潜在的原因,并暂时排除了这些原因:

  1. springboot应用不扫描controller所在的包?
    我把springboot application class移到了uplevel包里,也指定了它应该扫描什么样的包。 当你看到时,它不起作用。
  2. 注释问题?
    有人说我们应该使用@RestController 注释而不是@Controller,最好添加@ResponseBody,而我这样做了。 也没用。
  3. 部署问题?
    不知道,我确实改了这个项目的构建路径,正常生成了jar文件和war文件,但是webapp文件夹是空的,因为我没有任何静态资源,我觉得还可以,对吧?

谁能帮帮我?非常感谢您的回答,提前致谢。

从日志中,我可以看到您的 war 文件名为 library-Management,这意味着它已在该文件夹下部署,如日志所述。所以你应该检查 http://localhost:8080/library-Management/api/books 而不是。

您从 Tomcat 收到 404,它根本没有命中您的应用程序,这是因为您没有看到任何日志。