如何使用 Jersey 2.26 客户端通过 queryParam 进行 HTTP POST 请求?

How to do HTTP POST request with queryParam using Jersey 2.26 client?

我正在使用 Jersey 客户端来处理请求。这是一个例子。

https://myschool.com/webapi/rest/student/submitJob?student={student:[{"id":1,"name":"Tom","age":20},{"id":2,"name":"Bob","age":20}]}&score={score:[{"id":1,"math":90,"art":80,"science":70},{"id":2,"math":70,"art":60,"science":80}]}

响应会是这样的: {"jobId":"123456789","jobStatus":"JobSubmitted"}

这是我当前的代码:

String student = {student:[{"id":1,"name":"Tom","age":20},{"id":2,"name":"Bob","age":20}]};
String score = {score:[{"id":1,"math":90,"art":80,"science":70},{"id":2,"math":70,"art":60,"science":80}]}

String responseResult = client.target("https://myschool.com/webapi/rest/student/").path("submitJob")
                        .queryParam("student", student).queryParam("score", score).request("application/json").get(String.class);

问题是真正的请求URI太长导致我收到414错误。所以我需要使用 POST 而不是 GET 方法。但是我使用 queryParam 而不是 Body 发送请求。谁能告诉我该怎么做?谢谢

Use POST Method and Set Content type as "application/x-www-form-urlencoded". POST method increases allowable url request limit.


 String student = {student:[{"id":1,"name":"Tom","age":20},{"id":2,"name":"Bob","age":20}]};
 String score = {score:[{"id":1,"math":90,"art":80,"science":70},{"id":2,"math":70,"art":60,"science":80}]};
 Client client = ClientBuilder.newClient();
 Form input = new Form();
 input.param("student", student);
 input.param("score", score);
 Entity<Form> entity = Entity.entity(input, MediaType.APPLICATION_FORM_URLENCODED);
 String url = "https://myschool.com/webapi/rest/student/submitJob";
 ClientResponse response = client.target.request(MediaType.APPLICATION_JSON_TYPE)
.post(entity);

此代码基于@MohammedAbdullah 的回答以及球衣文档的启发。

Client client = ClientBuilder.newClient();
WebTarget target = client.target("https://myschool.com/webapi/rest/student/").path("submitJob");
Form form = new Form();
form.param("student", student);
form.param("score", score);
String responseResult = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), String.class);