In this post, we’ll explore the process of creating a Spring Boot REST service specifically designed for the HTTP POST method. We’ll delve into the creation of a Spring Boot REST controller that caters to a web service via HTTP POST. The focus will be on detailing the implementation of a POST method and subsequently testing it using tools such as Postman and the CURL command. Throughout this demonstration, we’ll be utilizing the Spring Tool Suite for our Spring Boot project.

Step 1: Create Spring Boot Project

To start, open the Spring Tool Suite and initiate the creation of a Spring Boot Project. Navigate to the File menu, select New Spring Starter Project, and furnish essential details about the project, including its name, location, artifact, group, package name, description, and more. Proceed by pressing the “Next” button to choose project dependencies. On this page, opt for Spring Web dependencies. The inclusion of Spring Web will trigger the launch of a Tomcat web application server on port 8080. Subsequently, the Spring Boot project will automatically deploy the REST web service onto the Tomcat web server.

Step 2: Create POM.xml file with dependencies

Check the proper creation of the required dependencies by opening the pom.xml file. When generating a Spring Boot application in the Spring Tool Suite, the pom.xml file is automatically generated. If you select “Spring Web” as a dependency during the creation of a Spring Boot project, the following dependencies are automatically appended to the pom.xml file. Verify the dependencies within the pom.xml file to confirm that all essential libraries are correctly loaded into the Spring Boot application.

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

Step 3: Create Rest Controller class

Expand the project structure within the Spring Boot application to access the src/main/java folder, which contains the default java package. Within this package, create a new Java class named TestController.java. This class will serve as the Rest Controller responsible for exposing the API for the REST call. The definition of the REST web service API will be outlined within this controller class. Add the annotation @RestController to the class, signaling its role as a REST controller. The following example illustrates the structure of the REST controller class:

package com.test;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

}

Step 4: Create a POST Rest API Method

The POST REST API is a Java method implemented within the controller class and made accessible through the HTTP POST method. To configure the HTTP POST method, the @PostMapping annotation is utilized. This annotation specifies the URL of the POST API as a parameter. It’s important to note that the POST API URL and the Java method name can differ. The POST API URL is hosted on the Tomcat server and can be accessed through a POST method call.

package com.test;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@PostMapping("HelloWorld")
	public String hello(@RequestBody String request) {
		return "Welcome Hello World!";
	}
}

In this example, the hello method is configured to handle POST requests at the “/HelloWorld” endpoint. The @RequestBody annotation is used to extract the request payload. Customize this method to suit the specific requirements of your POST API.

Step 5: Compile, Build and Run

While saving the Test Controller class, the Spring application will be automatically compiled within the Spring Tool Suite. The Maven pom.xml file is leveraged to build the Spring Boot application. Alternatively, you can employ the Maven command line to compile the Spring Boot project. To start the Spring Boot application, choose the “Run” menu option from the Run Menu.

Ensure that the required dependencies are resolved, and the application is successfully compiled and started. The Spring Boot application will start, and the REST APIs, including the one configured for the HTTP POST method, will be accessible through the specified endpoints.

Step 6: Invoke Rest API from Browser

When making HTTP POST API calls, it’s important to note that browsers typically send GET requests when you directly enter a URL in the address bar. As a result, invoking HTTP POST APIs directly from a browser’s address bar may lead to unexpected behavior.

To properly test HTTP POST APIs, it’s recommended to use dedicated tools such as Postman or command-line tools like cURL. These tools allow you to explicitly configure the HTTP method, including specifying POST for the desired API endpoint. This way, you can ensure that the requests align with the intended HTTP methods defined in your REST controller.

Step 7: Rest API using CURL command

The CURL command can be utilized to directly access the POST REST API calls. While Linux and Mac typically have CURL pre-installed, Windows users need to install the CURL package to run the curl command from the command line.

Please note that you may need to navigate to the directory where CURL is installed or provide the full path to the curl executable if it’s not in the system’s PATH. Additionally, ensure that the Spring Boot application is running and reachable at the specified URL before making API calls.

Here’s an example CURL command for making a POST request:

curl -X POST http://localhost:8080/your-post-endpoint -d "your_request_data"

or

curl --location --request POST 'http://localhost:8080/HelloWorld' -d "Yawin"

Replace your-post-endpoint with the actual endpoint and provide the necessary request data in place of your_request_data.

The output will be as shown below.

Welcome Hello World!

Step 8: Rest API using PostMan

In Postman, follow these steps to create a new POST request:

  1. Open Postman and click on the “New” button to create a new request.
  2. In the request tab, enter the URL in the address bar, specifying the endpoint for your POST request.
  3. Change the HTTP Method from GET to POST using the dropdown menu next to the address bar.
  4. If your POST request requires headers or body parameters, you can add them in the respective sections.
  5. After configuring the request, click the “Send” button to invoke the REST web service.

Postman will display the response from the server, and you can inspect the result, status, and headers. This allows you to test your Spring Boot application’s POST API easily and analyze the responses.

Leave a Reply