Java 如何使用Jersey 2.26客户端使用queryParam执行HTTP POST请求?

Java 如何使用Jersey 2.26客户端使用queryParam执行HTTP POST请求?,java,http-post,jersey-2.0,http-get,jersey-client,Java,Http Post,Jersey 2.0,Http Get,Jersey 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,"sci

我正在使用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发送请求。谁能告诉我怎么做?谢谢。

使用POST方法并将内容类型设置为“application/x-www-form-urlencoded”。POST方法增加了允许的url请求限制。
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);
字符串student={student:[{“id”:1,“name”:“Tom”,“age”:20},{“id”:2,“name”:“Bob”,“age”:20}]}; 字符串分数={分数:[{“id”:1,“数学”:90,“艺术”:80,“科学”:70},{“id”:2,“数学”:70,“艺术”:60,“科学”:80}]; Client Client=ClientBuilder.newClient(); 表单输入=新表单(); 输入参数(“学生”,学生); 输入参数(“分数”,分数); 实体=实体.实体(输入,MediaType.APPLICATION\u FORM\u URLENCODED); 字符串url=”https://myschool.com/webapi/rest/student/submitJob"; ClientResponse response=client.target.request(MediaType.APPLICATION\u JSON\u TYPE) .员额(实体);
此代码基于@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);

当我尝试您的代码时,在最后一行代码中出现了一个错误“resource(url)”。也许我有误会,你能解释一下吗@Mohammedabdullah请立即检查。错误消息:“类型客户端的方法资源()未定义”@穆罕默德杜拉