在Spring Boot应用程序中未调用SpringMVC Controller

时间:2018-10-20 09:57:45

标签: spring spring-boot

我已经使用spring boot创建了一个简单的spring mvc应用程序。但是,当我尝试调用localhost:8080时,它没有调用此应用程序的控制器。我需要知道我在哪里犯任何错误。我在下面提到了我的代码:

控制器类:

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>

主类:

   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);
    }
    }

在application.properties中,我什么都没有。当我尝试调用上述url时,没有任何反应。我希望我的控制器被呼叫。一旦控制器被调用,我将配置其他东西。请让我知道我在哪里犯任何错误。预先感谢。

Gopal Lal

2 个答案:

答案 0 :(得分:0)

问题是未扫描您的bean控制器:

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

如果您没有在SpringBootApplication上添加@ComponentScan(“ com.gopal”),则默认情况下它将扫描其所在的包:“ com.gopal.restdemo”。

如果您将SpringBootApplication移到它们的通用包中,那么您的问题将得到解决:

将RestdemoApplication移至 com.gopal

答案 1 :(得分:0)

请您尝试使用@ComponentScan如下所示:

   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);
    }
    }

并尝试通过GET请求使用localhost:8080 /