spring-bootcontroller

@Controller is not working in my spring boot application


This was my SpringBootWebApplication class

package com.learning.SpringBootWeb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWeb1Application {

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

}

This is my HomeController class

package com.learning.SpringBootWeb;

import org.springframework.stereotype.Controller;
import 'org.springframework.web.bind.annotation.GetMapping';

@Controller
public class HomeController {
    @GetMapping("/")
    public String home() {
        return "index.jsp";
    }
}

I had both the classes in the same package but still controller not getting recognized and i am getting the default error page.

What changes should i do to make work I do have proper dependencies added in my pom.xml file

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>10.1.34</version>
    </dependency>


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

Solution

  • Make sure to maintain this package structure:

    src/
      main/
        java/
          com/
            learning/
              SpringBootWeb/
                SpringBootWeb1Application.java
                HomeController.java
        webapp/
          WEB-INF/
            index.jsp