如何在 Jersey 客户端中使用 json body 请求 Delete 方法
how to ewquest Delete method with json body in Jersey client
我正在编写一个测试程序来测试球衣客户端是否有 json 正文。我收到邮递员的回复,并试图从 java jersey 客户端调用它,但收到错误消息
java.lang.IllegalStateException: Entity must be null for http method DELETE.
我如何请求删除带有 json body in jersey 的端点。
我尝试了以下客户端:
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);
}
}
Jersey 阻止使用 DELETE 请求发送数据。
如果您确实需要这样做,您可以像这样配置客户端:
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
Client client = ClientBuilder.newClient(clientConfig);
我正在编写一个测试程序来测试球衣客户端是否有 json 正文。我收到邮递员的回复,并试图从 java jersey 客户端调用它,但收到错误消息
java.lang.IllegalStateException: Entity must be null for http method DELETE.
我如何请求删除带有 json body in jersey 的端点。
我尝试了以下客户端:
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);
}
}
Jersey 阻止使用 DELETE 请求发送数据。
如果您确实需要这样做,您可以像这样配置客户端:
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
Client client = ClientBuilder.newClient(clientConfig);