1

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

3
  • It would help a lot if post the main class and the boot log. Commented Oct 20, 2018 at 10:22
  • I have just updated it. Have a look, please Commented Oct 20, 2018 at 10:36
  • Hi Gopal, i have added the solution which might help you to solve your problem. please have a look. Commented Oct 20, 2018 at 10:56

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Awesome. I tried both ways and it's working fine. Thanks a lot @Adina

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.