New to spring boot and looking to build your first program? Check this article to learn how to start your spring boot journey with a traditional Hello World program. Learn about Spring’s initializer project.
In this article we will write a Hello World application in Spring boot. We will need the following tools in order to do that.
- Java 8 or later
- Spring boot 2.x
- Maven 3.x
- Any IDE, STS or IntelliJ. We will use IntelliJ.
Following is the agenda for this
- Download or create the project structure.
- Write controllers
- Run the application in the IDE
- Run the application using executable jar.
Download or create the project structure
There are 2 ways you can create a folder structure for a Spring boot project.
- Download folder structure from official Spring Initializr homepage at https://start.spring.io/. Fill in the required fields like project, language, Spring Boot version, group, artifact, packaging and dependencies. You need to search for the required dependencies in there. Once you download the project source, import it in your IDE, for ex. Import as maven module.
- Install a plugin in the IDE and create from there. In IntelliJ ultimate edition, the Spring Initializr is already there which simulates the same form which the official homepage offers.
For the sake of simplicity, we will go with the IntelliJ version where the Spring Initializr is already there.
Steps to create the project structure
Click File -> New -> Module and you will see the following screen. Select Default and click Next.
Now, fill in the meta data of the project below.
Choose the required dependencies for your project. For our simple web application, Spring Web dependency is enough. Click Next and click Finish in the next screen.
Now the project with source has been created in the IDE but it is not detected by IDE as a maven project. To make it look like a maven project, you need to add framework support to it which is very simple. Right click on your project and click ‘Add Framework Support’. Select Maven and click OK. See below screen.
Now maven support has been added to your project and it looks like this.
You can open pom.xml and see that the dependency spring-boot-starter-web is there which corresponds to Spring Web which you chose while creating project structure in Spring Initializr.
The class HelloWorldApplication
is automatically created by Spring Initializr and this class is the entry point to run this boot application. This class contains the following code.
package com.example.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
So far, the project has been setup. Let’s write some real code below.
Write controllers
Create a controller class and annotate it with @RestController
. @RestController
is extension of Spring MVC’s @Controller
which adds @ResponseBody
to it and serializes the returned object into HttpResponse.
package com.example.helloworld;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public String hello() {
return "hello world";
}
}
See the URL mapping of the hello(). The returned string should be available at this URL path.
Run the application in the IDE
Run the class HelloWorldApplication
and see the console.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.2)
2022-01-07 21:08:46.616 INFO 14444 --- [ main] c.e.helloworld.HelloWorldApplication : Starting HelloWorldApplication using Java 1.8.0_265 on DESKTOP-78P411B with PID 14444 (D:\Inter\hello-world\target\classes started by engin in D:\Inter)
2022-01-07 21:08:46.616 INFO 14444 --- [ main] c.e.helloworld.HelloWorldApplication : No active profile set, falling back to default profiles: default
2022-01-07 21:08:47.553 INFO 14444 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-01-07 21:08:47.571 INFO 14444 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-01-07 21:08:47.575 INFO 14444 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-01-07 21:08:47.686 INFO 14444 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-01-07 21:08:47.686 INFO 14444 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 998 ms
2022-01-07 21:08:48.135 INFO 14444 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-01-07 21:08:48.148 INFO 14444 --- [ main] c.e.helloworld.HelloWorldApplication : Started HelloWorldApplication in 1.926 seconds (JVM running for 2.768)
2022-01-07 21:13:17.633 INFO 14444 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-01-07 21:13:17.633 INFO 14444 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-01-07 21:13:17.633 INFO 14444 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
In the logs you can see the line saying ‘Started HelloWorldApplication…’. This means that your boot application has started successfully. Now you can hit the following URL and see the result.
Hit http://localhost:8080/hello
Output
Run the application using executable jar
Build the project using maven by running the below command at the root of the project.
D:\Inter\hello-world>mvnw clean package
When you get the BUILD SUCCESS message, you will see that a jar has been created in the target folder.
Run this jar by running the below command.
D:\Inter\hello-world>java -jar target\hello-world-0.0.1-SNAPSHOT.jar
You will see the logs and application has started. Hit the URL http://localhost:8080/hello and you will see the “hello world” output.