Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 "{}";
    }
}
like image 365
Guillaume LUNIK Avatar asked Oct 20 '16 18:10

Guillaume LUNIK


2 Answers

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.

like image 53
mufeez-shaikh Avatar answered Oct 22 '22 10:10

mufeez-shaikh


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

}

like image 29
SantiagoM Avatar answered Oct 22 '22 12:10

SantiagoM