How to Make Post Request in Java Spring? - GeeksforGeeks

31 Aug, 2021
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using JAVA is that Java tries to connect every concept in the language to the real world with the help of the concepts of classes, inheritance, polymorphism, etc.
There are several other concepts present in java that increase the user-friendly interaction between the java code and the programmer such as generic, Access specifiers, Annotations, etc these features add an extra property to the class as well as the method of the java program. In this article, we will discuss how to make post request using PostMapping() annotation in the SpringBoot
PostMapping() annotation mainly use in the spring boot applications that are used for handling  the post request that is made by the client containing the JSON data in the header 
Initialising the Spring web in the project
Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model. This model allows us to configure the list of dependencies that are supported by JVM. Here, we will create the structure of an application using spring initializer and then use an IDE to create a sample GET route. 
The steps required are as follows:
Go to Spring InitializrFill in the details as per the requirements. 
Go to Spring Initializr
Fill in the details as per the requirements. 
Let us consider the below application, henceforth for this application it is as follows:
Project: Maven Language: Java Spring Boot: 2.2.8 Packaging: JAR Java: 8 Dependencies: Spring Web 
Step 1: Click on Generate which will download the starter project. 
Step 2: Extract the zip file. Now open a suitable IDE and then go to File->New->Project from existing sources->Spring-boot-app and select pom.xml. Click on import changes on prompt and wait for the project to sync. 
Note: 
In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.
Step 3: Go to src->main->java->com.gfg.Spring.boot.app, create a java class with the name Controller and add the annotation @RestController.Project Structure will look like.
The project Structure looks like as shown below:
Example 1: Controller.java
@RestController

public class Controller {

    // Handling post request
    @PostMapping("/EnterDetails")

    String insert(@RequestBody Details ob)
    {
        // Storing the incoming data in the list
        Data.add(new Details(ob.number, ob.name));

        // Iterating using foreach loop
        for (Details obd : Data) {
            System.out.println(obd.name + " " + ob.number);
        }
        return "Data Inserted";
    }
}
Example 2: Details.java
public class Details {

    // Creating an object of ArrayList
    static ArrayList<Details> Data
        = new ArrayList<Details>();
    int number;
    String name;
    Details(int number, String name)
    {
        // This keyword refers to parent instance itself
        this.number = number;
        this.name = name;
    }
}
This application is now ready to run. Run the Springbootapp class and wait for the Tomcat server to start.
Note: The default port of the Tomcat server is 8080 and can be changed in the application.properties file.
In between Postman is an API development tool that helps to build, test, and modify APIs. Almost any functionality that could be needed by any developer is encapsulated in this tool. It is used by over 5 million developers every month to make their API development easy and simple. It has the ability to make various types of HTTP requests(GET, POST, PUT, PATCH), saving environments for later use, converting the API to code for various languages(like JavaScript, Python). 
Now we will use Postman for making the post request and we can send the data in the form of JSON which is as shown below
Postman making post request
Now Press the send button as shown in the above pic we will get the below output:
This output will be generated on the console
Java-Spring
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Hashtable in Java
Constructors in Java
Different ways of Reading a text file in Java
Comparator Interface in Java with Examples
Java Math random() method with Examples
HashMap containsKey() Method in Java
How to Create Array of Objects in Java?
Convert Double to Integer in Java
Iterating over ArrayLists in Java
Generating random numbers in Java