簡體   English   中英

GetMapping 到 Spring Boot 中的方法不起作用

[英]GetMapping to Method in Spring Boot Isn't Working

我正在跟隨一個帶有 maven 的 SpringBoot 教程,當我嘗試將 index() 方法與@GetMapping時,當程序運行時,我只能在本地主機上看到這個:

Whitelabel Error Page 此應用程序沒有明確的 /error 映射,因此您將其視為后備。

Sun Dec 04 23:59:08 EST 2022 出現意外錯誤(類型=未找到,狀態=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

我試着環顧四周,但找不到任何東西,因此將不勝感激任何幫助。

您需要在@GetMapping 的參數中提供路徑。 示例:@GetMapping(path = "/abc")

然后進入本地瀏覽器的路徑:http://localhost:{port}/abc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM