Programmers learn & share
0 votes
445 views

Problem :

I am trying to write Spring boot code
The Program is running but I am getting following error:

This application has no explicit mapping for /error, so you are seeing this as a fallback.    

How to fix this error?

by (6.9k points)   | 445 views

1 Answer

0 votes

Solution :

When we are creating a Spring boot application we must annotate it with @SpringBootApplication annotation. This annotation will 'wrap up' many other necessary annotations for the application to work smoothly without errors. One such annotation as @ComponentScan annotation .Use of this annotation is to tell the Spring to look for all required Spring components and configure the application to run smoothly.

Application class must be present at the top of your package hierarchy, to allow Spring to scan sub-packages and look for the other required components.

Please find the sample code for your reference as follows:

package com.test.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

Following code snippet works like the controller package is under com.test.spring.boot package

package com.test.spring.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyHomeController {
    @RequestMapping("/")
    public String myhome(){
        return "Hello World!";
    }
}

Following code snippet will NOT Work as the controller package as it is NOT under com.test.spring.boot package

package com.test.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyHomeController {
     @RequestMapping("/")
     public String myhome(){
         return "Hello World!";
     }
 }

 

If you follow above given approach your issue will be resolved.

by (36.1k points)  
2,245 questions
2,807 answers
60 comments
241 users