Programmer
Programmer

Reputation: 167

how to ewquest Delete method with json body in Jersey client

I am writing a test program to test the jersey client to have json body in it. I am getting the response with postman and trying to call it from java jersey client but getting error as

java.lang.IllegalStateException: Entity must be null for http method DELETE.

How can I request delete endpoint with json body in jersey.

I tried the following client:

import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;


public class First {
    public static void main(String[] args) {
        String str = "{\n" +
                "\t\"ecid\": \"1\",\n" +
                "\t\"customerModelKey\": \"195000300\",\n" +
                "\t\"customerModelName\": \"A\",\n" +
                "\t\"customerGroupCode\": \"BI\"\n" +
                "}";
        System.out.println("test");
        Client client = ClientBuilder.newClient();
        WebTarget webTarget
                = client.target("http://localhost:9092/");
        WebTarget employeeWebTarget
                = webTarget.path("deletemodelecidrel");
        Invocation.Builder invocationBuilder
                = employeeWebTarget.request(MediaType.APPLICATION_JSON);
       Invocation invocation
                = invocationBuilder.build("DELETE",Entity.text(str));

       Response response = invocation.invoke();
        System.out.println(response);
    }
}

Upvotes: 3

Views: 2700

Answers (1)

Lucas Declercq
Lucas Declercq

Reputation: 1730

Jersey prevents sending data with a DELETE request.

If you really need to do that, you can configure the Client like this :

ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
Client client = ClientBuilder.newClient(clientConfig);

Upvotes: 4

Related Questions