Home > Software design >  GetMapping to Method in Spring Boot Isn't Working
GetMapping to Method in Spring Boot Isn't Working

Time:12-06

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:

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Dec 04 23:59:08 EST 2022 There was an unexpected error (type=Not Found, status=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

<?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.

CodePudding user response:

You need to provide path in the argument of @GetMapping. Example: @GetMapping(path = "/abc")

And then go into that path of your local browser: http://localhost:{port}/abc

  • Related