SpringBoot - Unable to start embedded container

Try annotating your SpringBootLoginController class with @SpringBootApplication annotation.

@SpringBootApplication
@RestController
public class SpringBootLoginController {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!!!";
    }

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

I deleted my .m2 repo, changed my version from 1.3.1 to 2.1.3 for the below, did a maven clean, re-build the project and it ran first time (using intellij)

parent>
   <groupId>org.springframework.boot</groupId>
   <version>2.1.3.RELEASE</version>
   <artifactId>spring-boot-starter-parent</artifactId>
</parent>

Annotating with the @SpringBootApplication resolves this issue.

@SpringBootApplication
@RestController
public class SpringBootLoginController {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!!!";
    }

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

Alternatively , by Adding the @EnableAutoConfiguration , @ComponentScan and @Configuration also resolves this issue.

@EnableAutoConfiguration
@RestController
public class SpringBootLoginController {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!!!";
    }

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

For my case, I was developing a command line project with springboot.

@SpringBootApplication
public class Application implements CommandLineRunner {
//my code here
}

So I was just using the simple starter.

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
 </dependency>

But I also got this error, and there was no web related dependency in my pom which was really wired.

And at last I found out that one of my dependency project was using the "javax.servlet.Servlet" in its own pom.

If you check the source code of the springboot, it will check if there is any "javax.servlet.Servlet" in your project when starting the application. And try to start a web "embedded container" when there is any "javax.servlet.Servlet".

That's why I got this error because I was using the "spring-boot-starter" and there was no web container in it.

So the solution is very simple, just tell the springboot that this is not a web project in the "application.properties":

spring.main.web-environment=false