【问题标题】:Spring Boot REST not showing or linking to RESTController page/classSpring Boot REST 未显示或链接到 RESTController 页面/类
【发布时间】: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


【解决方案1】:

只需删除类级别的请求映射,它就会起作用。

您可以做的另一件事是不要在类级别请求映射中添加任何上下文基础 url,如“root”,而是让它像 @RequestMapping("/") 并在 application.properties 文件中添加 server.servlet.context-path=/root

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多