Spring Boot Hello World Application with Intellij IDEA
We are going to create a sample web application that has only one API which returns a Hello World text. This application is created using Spring Boot and Intellij IDEA Spring initializr.
So, first we will create a new project on File->New->Project.
On the left pane, choose Spring Initializr and select your SDK version, then click Next.
In the next window we will define group and artifact values of our project and also give it a name and description. If you write the project name with dashes as in the below picture, your main Application class will be automatically created in camel case format, in our case it’s going to be SpringBootExampleApplication.
After you click next, a new window will be opened and you can choose your dependencies here. For this project I just choosed Spring Web. Now you can Next and create the project.
You can see that whole project structure is already created and also our main Application class is created as below.
You can start your application and connect it on http://localhost:8080, but you will see an error message because there is no service yet. So now we will create our controller for our Hello World service. I’ve created a new package named as ‘web’ and create a new Java class under that package. I named the class as ‘HelloWorldController’, you can choose any name you like.
First we need to define this class as a RestController, so add ‘@RestController’ annotation before class definition. Then add a new method to handle the request and return a response, I’ve added a method named sayHello.
‘@RequestMapping’ maps our method to a specific path, in our case our API path will be ‘/hello’. And also this API will have one request parameter which is ‘name’. When you run the application and type your browser ‘*http://localhost:8080/hello?name=World*’ you will see Hello World! message.
You can access the sample project on github repository below:
Written by Ahmet Kapusuz who lives and works in Amsterdam as a Software Engineer.