Search code examples
javaspringspring-bootmicroservices

Error in spring-boot appplication : This application has no explicit mapping for /error, so you are seeing this as a fallback


I have created a simple spring boot application in Eclipse which has User class and I have written web services to perform various operations on User such as get-users, create-user, delete-user.

When I am testing the application from the browser, I am getting the below error: This application has no explicit mapping for /error, so you are seeing this as a fallback.

The path that I used: http://localhost:8080/user/get-user/Mike I have annotated User resource class @RestController and Application class @SpringBootApplication.

Code snippet :

@RequestMapping(method = RequestMethod.GET, path = "/user/get-user/{name}" )
public User getUserByName(@PathVariable String name) {
    
    System.out.println("Request for name: "+name);
    return service.getUser(name); // this return the first user found with given name
}

Here is my project structure: Project Structure


Solution

  • Just had a look at your project structure. In your case, you need to ensure that your main class is in a root package above other classes.

    Whenever a Spring Boot Application is started or run (example : Application.java or a class annotated with @SpringBootApplication), it scans the classes below your main class package.

    An example would be :

    com
       +- test
             +- Application.java 
             |
             +- controller
             |   +- UserRestController.java
             +- domains
                 +- User.java
    

    Try this and check. Also, please ensure that you have added all the necessary annotations required on all the required classes for a Spring boot application for work correctly.