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 "{}";
    }
}
                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.
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);
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With