【发布时间】:2019-01-04 09:15:57
【问题描述】:
我在下面编写了这个 Spring Boot REST 基础应用程序,它启动了应用程序但无法调用或链接到 REST 控制器页面/类。
这是主类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan
public class SoccerglueApplication {
public static void main(String[] args) {
SpringApplication.run(SoccerglueApplication.class, args);
}
}
这是控制器类:
@RestController
@RequestMapping(value = "/root")
public class SoccerNewsAPI {
@GetMapping(value = "/in")
public String getMessage() {
return "Welcome to the Spring Boot Test";
}
}
【问题讨论】:
-
你在同一个包里有这个控制器吗?如果不是,您可以像这样创建此包的接口和点 @ComponentScan(basePackageClasses = [(DefaultCorePackage::class)])
-
从您的 SoccerglueApplication 中删除
@ComponentScan,因为它已在@SpringBootApplication中定义 -
我怀疑您没有遵循 Spring Boot 最佳实践。我怀疑您的
@SpringBootApplication注释类在另一个(子)包中,然后是您的@RestController。@SpringBootApplication带注释的类将扫描定义该类的所有包和子包。因此,如果@RestController位于未涵盖的包中,则它根本不存在。建议是将@SpringBootApplication注释类放在一个包中,该包涵盖所有子包。
标签: spring rest maven spring-boot web