如何在Jersey客户端中使用json主体进行ewquest删除方法

问题描述 投票:0回答:1

我正在编写一个测试程序来测试jersey客户端是否具有json主体。我收到邮递员的响应,并尝试从Java jersey客户端调用它,但收到错误,为

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

我如何请求在球衣中删除带有json正文的终结点。

我尝试了以下客户端:

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);
    }
}
java jersey-client
1个回答
0
投票

Jersey禁止发送带有DELETE请求的数据。

如果您确实需要这样做,可以像这样配置客户端:

ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
Client client = ClientBuilder.newClient(clientConfig);
© www.soinside.com 2019 - 2024. All rights reserved.