
Quick Contact
Java Tutorial
- What is Java?
- History of Java
- Hello Java Program
- Features of Java
- Basic Syntax
- Java Setup
- Data Types in Java
- Java Variables
- Operators in Java
- JVM
- Java If-else Statement
- Switch Case Statement
- Java Loops
- Do-While Loop
- Java While Loop
- Continue Statement
- Break Statement in Java
- Constructors in Java
- Oops Concept in Java
- Features of OOPs
- Inheritance
- Exeception handeling
- Aggregation (HAS-A relationship) in Java
- Method Overriding in Java
- Method Overloading
- Java Static Keyword
- Java This Keyword
- Java Final Keyword
- Polymorphism
- Static Binding and Dynamic Binding
- Abstract class in Java
- Access Modifiers in Java
- Difference between abstract class and interface
- Interface in Java
- Garbage Collection in Java
- Java Package
- Encapsulation
- Serialization and Deserialization in Java
- Java Inner Classes
- Java Applets
- Multithreading in Java
- Thread Priorities in Java
- Thread Creation
- Inter Thread Communication
- Wrapper Classes in Java
- Java Input Output
- Java AWT Introduction
- Java Layout Manager
- Java Layout Policy
- Java AWT Events
- Collection Framework
- Collection Framework List Interface
- Swing in Java
- Swing Utility Classes
- Swing Layout Managers
- Java JDBC
- Hibernate Framework Overview – Architecture and Basics
Springboot
- Spring Environment Setup
- Spring Boot CRUD REST API Project using IntelliJ IDEA | Postman | MySQL
- Dockerizing Spring Boot Application | Spring Boot Docker Tutorial
- spring-boot-restapidocumentation with swagger
- Spring Boot HttpClient Overview
- Apache HttpClient POST HTTP Request Example
- Apache HttpClient PUT HTTP Request Example
- Apache HttpClient DELETE HTTP Request Example
- Apache HttpClient HTML Form POST Request Example
- Spring Boot JSP Exampl
- Deploying Spring Boot WAR file with JSP to Tomcat
- Spring Boot Annotations
- Spring Core Annotations
- Spring MVC Annotations with Examples
- Spring Scheduling Annotations
- Spring - Java-based Container Configuration
- Spring Java Based Configuration Example
Hibernate
- Hibernate 5 hello world
- Hibernate- One to One Unidirectional Mapping Annotation Example
- Hibernate - Batch Processing
- Hibernate - Interceptors
- Hibernate 5 - Create, Read, Update and Delete (CRUD) Operations Example
- Hibernate Transaction Management
- Hibernate One to Many Unidirectional Mapping Example
- Hibernate One to Many Bidirectional Mapping Example
- Hibernate Many to Many Annotation Mapping Example
- Hibernate Primary KeyJoin Column
- Hibernate First Level Cache with Example
- Hibernate XML Configuration Example with Maven + Eclipse + MySQL Database
- Hibernate Java Configuration Example
- JPA 2 with Hibernate 5 Bootstrapping Example
- JPA and Hibernate Cascade Types
- Hibernate/JPA - Primary Key Generation
- Hibernate 5 - Enum Type Mapping Example
- Hibernate Component Mapping
- Hibernate Object States – Transient,Persistent and Detached
- Hibernate 5 - Save an Entity Example
- Hibernate 5 - Persist an Entity Example
- Hibernate 5 - saveOrUpdate() Method Example
- Hibernate 5 - get(), load() and byId() Method Examples
- Hibernate 5 - merge() Example
- Hibernate 5 - Delete or Remove an Entity Example
- Hibernate 5 - load() Method Example
- Hibernate Session Interface Methods
- Hibernate Session.clear() Method Example
- Introduction Of Java strutes to Architecture
- Struts 2 - Architecture
- Struts 2 - Configuration Files
- Struts 2 - Actions
- Struts 2 - Interceptors
- Struts 2 - Results & Result Types
- Struts 2 - Value Stack/OGNL
- Struts 2 - File Uploading
- Struts 2 - Database Access
- Struts 2 - Validations Framework
JAVA FX
- JavaFX Tutorial
- Introduction to JavaFX Pane
- JavaFX Popup
- JavaFX group
- JavaFX Controller
- JavaFX Gradient Color
- JavaFXAnchorPane
- JavaFXTabPane
- JavaFX Scene
- JavaFX Stage
- JavaFXWebView
- JavaFX Timeline
- JavaFX Timer
- JavaFX Image
- JavaFX Background
- JavaFX dialog
- JavaFX Font
- JavaFXTextArea
- JavaFXObservableList
- JavaFX GUI
- JavaFX FXML
- JavaFXEventHandler
- JavaFXGradle
- JavafxScrollpane
- JavaFXAPI
Apache HttpClient DELETE HTTP Request Example
In this quick article, we will discuss step by step how to use Apache HttpClient 4.5 to make an HTTP DELETE request. The HTTP DELETE Request Method requests delete the resource specified by the URI.
HttpClient supports out of the box all HTTP methods defined in the HTTP/1.1 specification: GET, HEAD, POST, PUT, DELETE, TRACE, and OPTIONS. There is a specific class for each method type.: HttpGet, HttpHead, HttpPost, HttpPut, HttpDelete, HttpTrace, and HttpOptions.
In this example, we will use HttpDelete class to handle DELETE HTTP method.
Using the Apache HttpClient
The Apache HttpClient library allows handling HTTP requests. To use this library add a dependency to your Maven or Gradle build file. You find the latest version here: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
We use maven to manage our dependencies and are using Apache HttpClient version 4.5. Add the following dependency to your project in order to make HTTP DELETE request method.
< groupId>org.apache.httpcomponents< /groupId> < artifactId>httpclient< /artifactId> < version>4.5< /version> < /dependency>
Development Steps
Let’s create a step by step example to make an Http DELETE request using HttpClient.
Create instance of CloseableHttpClient using helper class HttpClients.
CloseableHttpClienthttpclient=HttpClients.createDefault()
The HttpClients.createDefault() method creates CloseableHttpClient instance with default configuration.
Create basic DELETE request
HttpDeletehttpDelete=newHttpDelete(“http://localhost:8080/api/v1/users/5”);
Create a custom response handler
ResponseHandler< String>responseHandler= response -> { int status =response.getStatusLine().getStatusCode(); if (status >=200&& status <300) { HttpEntity entity =response.getEntity(); return entity !=null?EntityUtils.toString(entity) :null; } else { thrownewClientProtocolException("Unexpected response status: "+ status); } };
Send basic POST request via execute() Method
StringresponseBody=httpclient.execute(httpDelete, responseHandler);
HttpClient HTTP DELETE Request Method Example
Let’s discuss how to use HttpClient in real-time projects. Consider we have deployed Spring boot Restful CRUD APIs.
In the following example, we are using http://localhost:8080/api/v1/users/5 Rest service to delete a user from a database with id 5. In this example, we are using Java 7 try-with-resources to automatically handle the closing of the ClosableHttpClient and we are also using Java 8 lambdas for the ResponseHandler.
packagecom.tutorial.ducatindia.httpclient.examples;
importjava.io.IOException; importorg.apache.http.HttpEntity; importorg.apache.http.client.ClientProtocolException; importorg.apache.http.client.ResponseHandler; importorg.apache.http.client.methods.HttpDelete; importorg.apache.http.impl.client.CloseableHttpClient; importorg.apache.http.impl.client.HttpClients; importorg.apache.http.util.EntityUtils; /** * This example demonstrates the use of {@linkHttpDelete} request method. * * @authorRajanFadatare */ publicclassDELETERequestExample { publicstaticvoidmain(String[] args) throwsIOException { deleteUser(); } publicstaticvoiddeleteUser() throwsIOException { try (CloseableHttpClienthttpclient=HttpClients.createDefault()) { HttpDeletehttpDelete=newHttpDelete("http://localhost:8080/api/v1/users/5"); System.out.println("Executing request "+httpDelete.getRequestLine()); // Create a custom response handler ResponseHandler< String>responseHandler= response -> { int status =response.getStatusLine().getStatusCode(); if (status >=200&& status <300) { HttpEntity entity =response.getEntity(); return entity !=null?EntityUtils.toString(entity) :null; } else { thrownewClientProtocolException("Unexpected response status: "+ status); } }; StringresponseBody=httpclient.execute(httpDelete, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); } } }
Output
Executing request DELETE http://localhost:8080/api/v1/users/5 HTTP/1.1
—————————————-
{“deleted”:true}
More Examples
In the following example, we demonstrate the HTTP Delete request method by making an HTTP Delete Request Method to the following resource: http://httpbin.org/delete.
packagecom.tutorial.ducatindia.httpclient.examples;
importjava.io.IOException; importorg.apache.http.HttpEntity; importorg.apache.http.client.ClientProtocolException; importorg.apache.http.client.ResponseHandler; importorg.apache.http.client.methods.HttpDelete; importorg.apache.http.impl.client.CloseableHttpClient; importorg.apache.http.impl.client.HttpClients; importorg.apache.http.util.EntityUtils; /** * This example demonstrates the use of {@linkHttpDelete} request method. * * @authorRajanFadatare */ publicclassDELETERequestExample { publicstaticvoidmain(String[] args) throwsIOException { delete(); } publicstaticvoiddelete() throwsIOException { try (CloseableHttpClienthttpclient=HttpClients.createDefault()) { HttpDeletehttpDelete=newHttpDelete("http://httpbin.org/delete"); System.out.println("Executing request "+httpDelete.getRequestLine()); // Create a custom response handler ResponseHandler< String>responseHandler= response -> { int status =response.getStatusLine().getStatusCode(); if (status >=200&& status <300) { HttpEntity entity =response.getEntity(); return entity !=null?EntityUtils.toString(entity) :null; } else { thrownewClientProtocolException("Unexpected response status: "+ status); } }; StringresponseBody=httpclient.execute(httpDelete, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); } } }
Output
Executing request DELETE http://httpbin.org/delete HTTP/1.1
—————————————-
{ "args": {}, "data": "", "files": {}, "form": {}, "headers": { "Accept-Encoding": "gzip,deflate", "Connection": "close", "Content-Length": "0", "Host": "httpbin.org", "User-Agent": "Apache-HttpClient/4.5 (Java/1.8.0_172)" }, "json": null, "origin": "49.35.12.218", "url": "http://httpbin.org/delete" }
Apply now for Advanced Java Training Course