简体   繁体   中英

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 "{}";
    }
}

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM