2017-08-25 2 views
1

私はJerseyクライアントを使用してリクエストを行っています。ここに例があります。Jersey 2.26クライアントを使用してqueryParamでHTTP POSTリクエストを行う方法は?

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エラーが発生したことです。だから私はGETメソッドの代わりにPOSTを使う必要があります。しかし、私はqueryParamを使用して要求を送信しますが、Bodyは送信しません。それをどうやって誰に教えてもらえますか?ありがとう。

答えて

0

このコードは、@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); 
0
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); 
+0

コードを試したところ、最後のコード行に「resource(url)」というエラーがありました。たぶん私は誤解を持っていたかもしれません、あなたはそれを説明してください? @MohammedAbdullah – ZLi

+0

今すぐご確認ください。 –

+0

エラーメッセージ:「メソッドのリソース()は、タイプ「クライアント」に対して未定義です。 @MohammedAbdullah – ZLi

関連する問題