Home > Enterprise >  Sping Boot basic project not running in Intellij Community Edition Same code runs in STS
Sping Boot basic project not running in Intellij Community Edition Same code runs in STS

Time:08-29

I have created a Basic Spring Project with following three files in IntelliJ community Edition.

Starter Class :

package com.learn.spring;

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

@SpringBootApplication
public class StarterClass {

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

Hello.java

package com.learning.spring.hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Hello {

    @RequestMapping("/hello")
    public String hi() {
        return "Hello How Are you doing in IntelliJ";
    }
}

pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>SpringProjectIntelliJ28082022</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.14</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.14</version>
            </plugin>
        </plugins>
    </build>
</project>

When I run the application I get the following in my Console :

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.5.14)

2022-08-28 15:07:36.980  INFO 9408 --- [           main] com.learn.spring.StarterClass            : Starting StarterClass using Java 1.8.0_291 on ******** with PID 9408 (S:\Java_Workspaces\spring\SpringProjectIntelliJ28082022\target\classes started by **** in S:\Java_Workspaces\spring\SpringProjectIntelliJ28082022)
2022-08-28 15:07:36.981  INFO 9408 --- [           main] com.learn.spring.StarterClass            : No active profile set, falling back to 1 default profile: "default"
2022-08-28 15:07:37.912  INFO 9408 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-08-28 15:07:37.932  INFO 9408 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-08-28 15:07:37.932  INFO 9408 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.63]
2022-08-28 15:07:38.084  INFO 9408 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-08-28 15:07:38.084  INFO 9408 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1071 ms
2022-08-28 15:07:38.382  INFO 9408 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-08-28 15:07:38.382  INFO 9408 --- [           main] com.learn.spring.StarterClass            : Started StarterClass in 1.739 seconds (JVM running for 2.04)
2022-08-28 15:08:01.308  INFO 9408 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-08-28 15:08:01.308  INFO 9408 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-08-28 15:08:01.308  INFO 9408 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms

but when I open the browser and hit localhost:8080/hello

I get the error message webpage. enter image description here

Please guide me on where am i going wrong.

CodePudding user response:

Your Starter and Hello classes are in different packages (com.learn.spring vs com.learning.spring.hello), for Spring to automatically pick up your Hello class it must be in the same package as your Starter.

If the different packages are intended that way, you will need to tell Spring to scan them using ComponentScan annotation:

package com.learn.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.learning.spring")
public class StarterClass {

    public static void main(String[] args) {
        SpringApplication.run(StarterClass.class,args);
    }
}
  • Related