9

I am fairly new in Spring Boot and trying to build a simple web app. I have defined a controller class containing my mapping for url, but on browser it is giving me a white label page error(404). I am not able to understand why it is not able to map. I have tried changing my component scan base package, but it still doesn't redirect me to page "abc" or print "in controller" in the console.

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>bms</groupId>
    <artifactId>com.wellmanage.bms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>BMS</name>
    <description>Demo project for Spring Boot</description>

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

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

    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Controller class

package com.wellmanage.controller;   

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BookController {

    @RequestMapping("/")
    public String hello() {
        System.out.println("in controller");
        return "abc";
    }
}

Main class

package com.wellmanage.bms;

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

@SpringBootApplication
public class BmsApplication {

    public static void main(String[] args) {
        SpringApplication.run(BmsApplication.class, args);
    }
}
Daniel Olszewski
  • 13,995
  • 6
  • 58
  • 65
Gaurav Bipul
  • 93
  • 1
  • 1
  • 4

1 Answers1

29

The BmsApplication class is placed under the com.wellmanage.bms package and by default the @SpringBootApplication annotation runs component scan using this package as the root. Since the BookController is inside com.wellmanage.controller, the package is omitted by default configuration.

You have two options:

  1. Change default component scan settings and set which package would you like to scan:
    @ComponentScan("com.wellmanage")
    @SpringBootApplication
    public class BmsApplication {

        public static void main(String[] args) {
            SpringApplication.run(BmsApplication.class, args);
        }
    }
  1. Move the main Application class to root package of you app so that all components which you want to scan automatically are under its package, e.g. to com.wellmanage.

From the configuration you have posted, it seems you didn't set any template engine. Returning the String "abc" from your controller is hence ignored.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Daniel Olszewski
  • 13,995
  • 6
  • 58
  • 65
  • Thanks a lot,it worked but In a few other applications that i made using Spring Boot,I didn't had to explicitly give the base package name,even though controller was in a different class.Is it mandatory to always reconfigure "@ComponentScan"? – Gaurav Bipul May 20 '16 at 11:09
  • The package of the configuration class that is annotated with `@ComponentScan` (or `@SpringBootApplication` as it wraps `@ComponentScan`) is used as the root directory for component searching. If all component that you want to find automatically are under that root package you don't have to reconfigure `@ComponentScan`. In your case the root package was set to **com.wellmanage.bms**, hence controller inside **com.wellmanage.controller** couldn't be found, because the **controller** package is not under **bms**. – Daniel Olszewski May 20 '16 at 11:18
  • hello @DanielOlszewski, it also works if controller exist in another package in a multi module project? Thank you in advance – CodeSlave Feb 28 '22 at 09:14