Developing First Spring Boot Web Application
In this article, I am going to discuss How to Develop First Spring Boot Web Application. Please read our previous article where we discussed How to Set up the Development Environment for Developing Spring Boot Applications.
Verify the Installation of the Spring Boot
Once the project has been initialized and the dependencies are installed (in the pom.xml file), we can start developing the application.
Step 1: Navigate to the src/main/com.dotnet.helloworld folder:
Step 2: Open the HelloworldApplication.java file and verify that the configuration is as follows:
Step 3: Run the application using the run button at the top right of the window:
The output should be as follows:
This output shows that Spring, Java, and all the other extensions are correctly installed.
Write a REST Endpoint
Step 1: Add spring-boot-starter-web dependency to the pom.xml file. This can be done by typing into the pom.xml file. Add the following text into the dependencies section to add the dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Step 2: Import the following packages into the HelloworldApplication.java file:
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
Step 3: Add an @RestController annotation at the top of the class:
Step 4: Write a request URI method. URI stands for Uniform Resource Identifier. This is a unique arrangement of characters that identify a resource. Such URIs are typically used by web technologies. In this case, a request URI can be added using the @RequestMapping annotation. This request URI should return “Hello World!”.
Step 5: Run the application using the run button at the top right of the window. The following output must be displayed:
As can be seen from the output, the Tomcat server has been started at port 8080 on localhost.
Step 6: Verify the output. Open your web browser and type http://localhost:8080/ into the address. The following output must be displayed:
The text shall be displayed in the default font of the particular browser (Times New Roman in this case). This application is now running on a Tomcat server. Tomcat is a technology that allows for a virtual server to be created without the need for server hardware.
Step 7: Enter Ctrl-C into the terminal (in VS Code) to stop the server and the application. Once this is done, the webpage shall no longer be available:
Congratulations! You have developed your first web application using Spring Boot!
In the next article, I am going to discuss Spring Boot Tomcat Deployment. Here, in this article, I try to explain the Developing First Spring Boot Web Application. I hope you enjoy this How to Develop First Spring Boot Web Application article.