This demonstrates how to build a simple RESTful API using the Spring Boot framework, display the user data on an HTML page, and test it with Postman API.
Before starting this, make sure you have the following:
- Java Development Kit (JDK) installed
- Visual Studio Code (or any other IDE) installed
- Postman (or any other API testing tool) installed
Follow the steps below to set up the project and run the application:
-
Clone the repository or download the project files.
-
Open the project in Visual Studio Code.
-
Open a terminal in Visual Studio Code (View -> Terminal).
-
Run the following command in the terminal to build and run the application:
./mvnw spring-boot:run
This will start the Spring Boot application and listen for requests on
http://localhost:8080
. -
Open Postman and create a new request.
-
Set the request method to
GET
and enter the following URL:http://localhost:8080/user
This will retrieve the user data from the API.
-
The response should contain the user's name and email.
-
Open a web browser and navigate to
http://localhost:8080/index.html
. -
The HTML page will display the user's name and email obtained from the API.
The project structure is as follows:
demo/
├── src/
│ └── main/
│ ├── java/
│ │ └── com/
│ │ └── example/
│ │ └── demo/
│ │ ├── controller/
│ │ │ └── UserController.java
│ │ └── DemoApplication.java
│ └── resources/
│ └── static/
│ └── index.html
└── pom.xml
-
The
UserController
class in thecontroller
package defines the API endpoint for retrieving user data. -
The
DemoApplication
class in the root package is the main entry point of the application. -
The
index.html
file in thestatic
directory is the HTML page that displays the user data.
To customize the user data, follow these steps:
-
Open the
UserController
class (src/main/java/com/example/demo/controller/UserController.java
). -
Modify the
getUserInfo
method to return the desired user data. You can change the default values for thename
andemail
parameters or fetch the data from a database or external service. -
Rebuild and run the application to see the updated user data.
You can test the API endpoints using Postman or any other API testing tool.
This project is licensed under the GNU License.