[英]GetMapping to Method in Spring Boot Isn't Working
I'm following along with a SpringBoot tutorial with maven, and when I try to map the index() method with @GetMapping
, when the program is run, I can only see this on localhost:我正在跟随一个带有 maven 的 SpringBoot 教程,当我尝试将 index() 方法与
@GetMapping
时,当程序运行时,我只能在本地主机上看到这个:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Whitelabel Error Page 此应用程序没有明确的 /error 映射,因此您将其视为后备。
Sun Dec 04 23:59:08 EST 2022 There was an unexpected error (type=Not Found, status=404). Sun Dec 04 23:59:08 EST 2022 出现意外错误(类型=未找到,状态=404)。
This is my code:这是我的代码:
@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!";
}
}
This is my pom.xml file这是我的 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>
Here is my console output:这是我的控制台输出:
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
I tried looking around, but couldn't find anything, so any help would be greatly appreciated.我试着环顾四周,但找不到任何东西,因此将不胜感激任何帮助。
You need to provide path in the argument of @GetMapping.您需要在@GetMapping 的参数中提供路径。 Example: @GetMapping(path = "/abc")
示例:@GetMapping(path = "/abc")
And then go into that path of your local browser: http://localhost:{port}/abc然后进入本地浏览器的路径:http://localhost:{port}/abc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.