Spring Boot 中的 GetMapping 方法不起作用

问题描述 投票:0回答:2

我正在使用maven跟随SpringBoot教程,当我尝试使用

@GetMapping
映射index()方法时,当程序运行时,我只能在本地主机上看到这个:

Whitelabel 错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。

2022 年 12 月 4 日星期日 23:59:08(美国东部时间) 出现意外错误(类型=未找到,状态=404)。

这是我的代码:

@SpringBootApplication
@RestController
public class DemoApplication {

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

       @GetMapping
       public String index() {
              return "Greetings from Spring Boot!";
       }
}

这是我的 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>3.0.0</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>
   <properties>
      <java.version>17</java.version>
   </properties>
   <dependencies>
<!--      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.postgresql</groupId>
         <artifactId>postgresql</artifactId>
         <scope>runtime</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

这是我的控制台输出:

2022-12-04T23:58:57.579-05:00  INFO 24656 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 17.0.4 with PID 24656 (/Users/ym/Downloads/demo/target/classes started by ym in /Users/ym/Downloads/demo)
2022-12-04T23:58:57.582-05:00  INFO 24656 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2022-12-04T23:58:57.980-05:00  INFO 24656 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-12-04T23:58:57.985-05:00  INFO 24656 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-12-04T23:58:57.986-05:00  INFO 24656 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.1]
2022-12-04T23:58:58.029-05:00  INFO 24656 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-12-04T23:58:58.030-05:00  INFO 24656 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 422 ms
2022-12-04T23:58:58.169-05:00  INFO 24656 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-12-04T23:58:58.174-05:00  INFO 24656 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.764 seconds (process running for 0.945)
2022-12-04T23:59:08.798-05:00  INFO 24656 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-12-04T23:59:08.799-05:00  INFO 24656 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-12-04T23:59:08.799-05:00  INFO 24656 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms

我尝试环顾四周,但找不到任何东西,所以我们将不胜感激。

java spring spring-boot maven spring-mvc
2个回答
1
投票

您需要在@GetMapping的参数中提供路径。 示例:@GetMapping(path = "/abc")

然后进入本地浏览器的路径:http://localhost:{port}/abc


0
投票

我的问题是类控制器的包错误,所以也检查一下

© www.soinside.com 2019 - 2024. All rights reserved.