Creating a simple backend application with Java(Spring boot) :Learning to code Java

Author: Praweg Koirala

🗓️ October 12, 2023

⌛️ 2 mins

🪴 Growing

🏷️    backend | Java

This will be a step by step walk through on how to create a simple Spring boot application with Java using intelliJ IDEA Community Edition. So it is assumed that Java is already installed and intelliJ IDE is already installed and ready to use. With that all out of the way , we can simply follow the steps outlined below and codes provided to build this simple, beginner friendly project.

  • We are using intelliJ IDEA Community Edition(IDE) for our Java(Spring boot) project.
  • Spring boot is not directly available on the Free community version of the IDE but already created spring boot project can be opened . Hence there is a work around for this problem.
  • We can create an initial spring boot project by using Spring Initializer @ https://start.spring.io/
  • Here under dependency add Spring Web as the dependency. Name, description and other parameters can be set as well but we will leave all default for simplicity
  • Now generate the project , this downloads a zipped file.
  • Unzip the file and open the unzipped file from IntelliJ IDEA.
  • In Project directory src>main>java>com.example.demo is a default java file called DemoApplication.java with following code .
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
  • If we run this code , it will start the TomCat Server by default at localhost:8080. without any home routes.
  • if we run this server it shows few error texts as there are no home route set. but the server is running
  • Next we create a home route. for this under the parent directory com.example.demo . we create a new file HomeController.java ( for this we can right click and create new , java class and give the name HomeController , this will create the file with the same class name as follows
package com.example.demo; public class HomeController { }
  • We will add code to create the route as follows, where (”/”) means home route and will display the message returned by the home() function when server is running home route (default route)
package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HomeController { @GetMapping("/") public String home() { return "Welcome! Your backend is now running on TomCat server with spring boot."; } }
  • Additional routes can be created by creating new classes with new function names and return message.
  • The default port of 8080 can be changed. to change this default port go to src>main>resources>application.properties file
  • here we can set the desired port as such:
server.port=4040
  • And now if we save and run application. the server will run on new port 4040 and serve the default home route with the set message .
Welcome! Your backend is now running on TomCat server with spring boot.

This post was last updated on: October 12, 2023

Back