Gopal Lal
Gopal Lal

Reputation: 51

SpringMVC Controller is not getting called in spring boot application

I have create one simple spring mvc application using spring boot. But when I am trying to call localhost:8080 its not calling controller of this application. I need to know, where I am doing any mistake. I have mentioned my code below:

Controller class:

package com.gopal.controllers;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gopal.models.Address;
import com.gopal.models.Employee;


@Controller
public class EmployeeMvcController {

   public EmployeeMvcController() {
      // TODO Auto-generated constructor stub
   }

@RequestMapping(value="/", method = RequestMethod.GET)
public String getEmployeeList(ModelMap modelMap) {
    System.out.println("starting controller");
    List<Employee> employees = new ArrayList<>();
    Employee e = new Employee();
    e.setFirstName("Gopal");
    e.setId(1);
    e.setAddress(new Address(1, "BRM College", "Nayatola, Raiser"));
    employees.add(e);
    modelMap.put("employee", e);
    System.out.println("ending controller");
    return "welcome.jsp";
}
}

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 
 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

<groupId>com.gopal</groupId>
<artifactId>restdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>restdemo</name>
<description>Rest Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-
    8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

Main class:

   package com.gopal.restdemo;

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

    @SpringBootApplication
    public class RestdemoApplication {

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

in application.properties, I have nothing. When I am trying to call above mentioned url, nothing is happening. I want my controller should get called. Once controller will get called, I will configure my other stuffs. Please let me know where I am doing any mistakes. Thanks in advance.

Gopal Lal

Upvotes: 1

Views: 2902

Answers (2)

GauravRai1512
GauravRai1512

Reputation: 844

can you please try with @ComponentScan like below:

   package com.gopal.restdemo;

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

    @SpringBootApplication
    @ComponentScan(basePackages={"com.gopal.controllers"})
    public class RestdemoApplication {

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

and try with localhost:8080/ with GET request

Upvotes: 2

Adina Rolea
Adina Rolea

Reputation: 2109

The problem is that your bean controller is not scanned:

package com.gopal.controllers;  -> controller package
com.gopal.restdemo;  -> spring boot application package

If you don't add @ComponentScan("com.gopal") on your SpringBootApplication, by default it will scan the package where it is located : "com.gopal.restdemo".

If you will move your SpringBootApplication in common package for both of them, your problem will be fixed:

Move RestdemoApplication to com.gopal

Upvotes: 0

Related Questions