Unable to run localhost in Spring Boot application (Tomcat server)

Viewed 33851

I've been learning Spring using the following tutorials: https://www.youtube.com/playlist?list=PLqq-6Pq4lTTbx8p2oCgcAQGQyqN8XeA1x

I have created a Maven project and tried to run the Spring Boot application while referring to these videos:

https://www.youtube.com/watch?v=E7_a-kB46LU&index=9&list=PLqq-6Pq4lTTbx8p2oCgcAQGQyqN8XeA1x

I tried to run my Spring application on Tomcat server but the localhost isn't working. (Port 8080)

My pom.xml looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.javabrains.springbootquickstart</groupId>
  <artifactId>course-api-new</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Java Brains Course API</name>

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

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

  <properties>
        <java.version>1.8</java.version>
  </properties>

</project>

CourseApiApp.java :

package io.javabrains.springbootstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CourseApiApp {

    public static void main(String[] args) {
        SpringApplication.run(CourseApiApp.class, args);

    }

}

According to the video localhost should display Whitelist error on running the application, but it doesn't run at all.

Any help would be appreciated, thanks!

11 Answers

Solution: add web starter dependency to pom.xml for maven

No need to create new app. Just change this starter dependency to starter-web dependency:

Starter

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

Starter Web

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

I also faced the same issue. Try creating Spring Boot apprlication by choosing Spring Boot starter project in STS. Right click on STS->New->Spring Starter Project->Give name and click on Next->Choose Web and tick mark on Web and search for Dev Tools and tick mark Dev Tools and click on Finish

Now click on the project and Run as Spring Boot Application.

In application.properties put server.port=8080

I faced the similar issue. I too was following the JavaBrains SpringBoot example. This is what I did: I created a fresh spring boot project using STS tool and included the same code as provided by you and it worked!

You need to tell springboot where do you have to scan your reference classes

@SpringBootApplication
@ComponentScan(basePackages= {"com.testbean.controller", "com.testbean.model", "com.testbean.service"}) 
public class DemoApplication {

Use @ComponentScan("io.javabrains") and mention the package in which the Controller is present. eg.

@SpringBootApplication
@ComponentScan("io.javabrains")
public class CourseApiApp {
    public static void main(String[] args) {
        SpringApplication.run(CourseApiApp.class, args);
    }
}

In a spring boot application, by default spring searches for components in the same package which is having the @SpringBootApplication annotation.

Follow this tutorial to setup JavaJDK, Tomcat and Maven: https://jeromejaglale.com/doc/spring4_tutorial/installation_macos

Next step move to Terminal app: type cd root_project and run sudo mvn spring-boot:run

Related