In this Spring Boot Restful webservice example, we demonstrate a basic GET API request that produces a plain text message and another request that furnishes a Java object in JSON format. Employing Tomcat as the application server and a rest controller, this Spring Boot Rest API example delivers a RESTful webservice.
Create Spring Boot RESTful Web Service Project
The Spring Tool Suite serves as the editor in this Spring Boot Restful web service API example. Start the Spring Tool Suite application, and then, from the file menu, proceed to create a Spring Starter Project. To do this, navigate to “New -> Spring Starter Project” in the file menu. The following image illustrates the menu option within the Spring Tool Suite for your reference.
Upon selecting “New -> Spring Starter Project” from the file menu, a popup window will emerge, prompting you to input project details. Provide essential information such as the project name, group, artifact, description, and package. For instance, designate the Spring Boot project as “Project Name,” and define a package name, which establishes a Java package encompassing all Java files. The Maven dependencies JAR is crafted utilizing the specified group and artifact details. Ensure accuracy in these entries for effective project configuration.
Once you’ve entered the project information, proceed by clicking the “Next” button to proceed to the selection of Maven dependencies. In the search bar, input “web,” and subsequently, choose “Spring Web” from the displayed options. The inclusion of Spring Web is pivotal for establishing a Tomcat web server. Note that, by default, the Tomcat server operates on port 8080, providing a foundational configuration for your Spring Boot project.
On the right side panel, you will observe the details related to the selected “Spring Web.” To advance to the Site Information window, click the “Next” button. It’s important to note that any information presented on the Site Info page cannot be modified at this stage; instead, it serves to display the site information associated with your Spring Boot project. Proceed with the “Next” button to continue with the project setup.
Complete the setup of the Spring Boot Starter Project by clicking the “Finish” button. This action triggers the creation of the Spring Boot application, and all specified Maven dependencies will be loaded accordingly. The resulting Spring Boot project structure will be established as illustrated in the below image, finalizing the process of creating your Spring Boot Starter Project.
Run Default Spring Boot Web Application
Within the Spring Boot application, select the “SpringBootRestAPI” application from the left panel. Subsequently, navigate to the Run menu and choose the “Run” menu item. The Spring Boot application will commence and run by default
Upon execution, the console window will showcase the starting log of the Spring Boot application. You will observe Tomcat being initiated and listening on port 8080. The log output provides valuable information, including the status of Tomcat and the specific port on which it is running. This comprehensive log ensures transparency regarding the initialization and runtime details of your Spring Boot application.
Creating Rest Controller Java Class
Expand the project within the Spring Boot application, and proceed to navigate to the source package named com.yawin
. This package is situated in the src/main/java
folder. Within the com.yawin
Java package, you should create a Rest Controller Java class. On the left panel, initiate a right-click action on the com.yawin
package name and choose “New -> Class” to create a new class under this Java package.
Within the Spring Tool Suite, start the creation of a new Java class by opening the corresponding window. Specify “TestController” as the name and proceed by selecting the “Finish” button, keeping default settings for the other options. The com.yawin
package will serve as the destination for the creation of the TestController.java
class, and you’ll observe the addition of “TestController.java” to the left panel.
Open the Java class in the right panel, and insert the provided TestController
code snippet. This establishes the foundation for the TestController class, facilitating the implementation of the specified functionality within your Spring Boot project.
package com.test; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @GetMapping("HelloWorld") public String hello() { return "Welcome Hello World!"; } }
After incorporating the Test Controller code, the page will exhibit a structure similar to the one depicted below. This visual representation showcases the TestController class within the Spring Tool Suite, confirming the successful addition of the specified code to your Spring Boot project.
Run First RESTful Webservice API Call
Execute the Spring Boot application after saving the TestController code. If the application is already running, ensure to stop and restart it. The console window will exhibit the startup log of the application.
Subsequently, open a web browser and input “http://localhost:8080/hello” in the address bar. By doing so, you will trigger your first REST webservice call, and the browser will display the welcome message. This marks the successful execution of your Spring Boot application and the initiation of your RESTful webservice.
Create RESTful Webservice with JSON Call
Return to the TestController.java
file and incorporate a new method called helloObject
according to the provided code snippet below. This additional method is created to provide a JSON response within the REST webservice.
package com.test; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @GetMapping("HelloWorld") public String hello() { return "Welcome Hello World!"; } @GetMapping("HelloWorldRest") public Message helloObject() { return new Message(1,"Welcome Hello World!"); } }
Restart the Spring Boot application to incorporate the modifications made to the TestController
class. Once the application is running, open your web browser and enter “http://localhost:8080/api/helloObject” in the address bar. The browser will present the JSON output, reflecting the changes implemented in the helloObject
method of your RESTful webservice.