Guillaume LUNIK
Guillaume LUNIK

Reputation: 169

Spring REST Controller not mapped

I'm trying to create à Rest controller that listen on "/login" I have defined the code bellow but when I open http://localhost:8080/login I get a 404 error... Please help :)

Here is my package structure:

com.my.package
   |_ Application.java
   |_ controller
          |_ LoginController

My Application:

@ComponentScan("com.my.package.controller")
@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

My Rest controller:

@RestController
public class LoginController {

    @RequestMapping("/login")
    public @ResponseBody String getLogin(){
        return "{}";
    }
}

Upvotes: 7

Views: 10919

Answers (3)

Deian Petrov
Deian Petrov

Reputation: 1

Sometimes, when it makes no sense why the mapping is not working, just change the server port so something different than 8080.

ie. in the application.properties or yml, server.port=8081

Upvotes: 0

mufeez-shaikh
mufeez-shaikh

Reputation: 199

The controller class should be in a folder of the Application class or in the lower folder.

So, if the application class is in package com.company.app, then the controller class should be in package com.company.app or in com.company.app.*. Let say the controller class is in com.company.controller, it will not mapped since its not in same package or child package of application class.

Upvotes: 6

SantiagoM
SantiagoM

Reputation: 73

You should use this annotations in your init class of your springBoot App

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.my.package")
public class WebAppInitializer{

    public static void main(String[] args) throws Exception{
        SpringApplication.run(WebAppInitializer.class, args);
    }

}

Upvotes: 1

Related Questions