Open In App

How to Make Delete Request in Spring?

Last Updated : 10 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

DeleteMapping() annotation is mainly used in the spring boot applications that are used for handling  the delete 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.

Steps required are as follows:

  1. Go to Spring Initializr
  2. Fill in the details as per the requirements.
  3. 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

Note: For illustration purposes, we will be considering 

Project: Maven 
Language: Java 
Spring Boot: 2.2.8 
Packaging: JAR 
Java:Dependencies: Spring Web 

Let us sequentially do describe these three steps as proposed above in the header as follows: 

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

// Annotation
@RestController

// Controller class
public class Controller {

    // Creating an empty ArrayList
    ArrayList<Integer> a = new ArrayList<>();

    // Constructor
    Controller() {
        a.add(1);
        a.add(2);
    }

    // Annotation
    @DeleteMapping("/hello/{id}")

    // Method
    public void deleteById(@PathVariable("id") int id) {

        a.remove(new Integer((id)));

        print();
    }

    // Method
    void print() {
        for (int elements : a) {
            System.out.print(elements);
        }
    }

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), save environments for later use, converting the API to code for various languages(like JavaScript, Python).

Lastly, we will use Postman for making the delete request and we can send the data in the form of JSON which is as shown in the below media as follows:

Output: As generated on console 

2021-10-14 1
2

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads