簡體   English   中英

如何將參數傳遞給javax.ws.rs.client.WebTarget.Builder Put方法

[英]How to pass parameters to javax.ws.rs.client.WebTarget.Builder Put method

我是Jax-RS和Jersey 2.0的新手。 這是我想要做的。

我有一個具有以下屬性的資源

id-字符串名稱-String注釋-字符串數組,我想為此添加新注釋

所以我有一個REST Web服務方法http:// host:port / / rest / resourceupdate此Web服務的主體必須是JSON對象

現在我正在嘗試構建客戶端代碼

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;
import  javax.ws.rs.PUT;
import javax.ws.rs.Consumes;

import org.apache.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

    void updateComments (String CommentToUpdate)

    {

        String url = "http://host:port/<appl>/rest/resourceupdate";
        //I am building a JSON object

        JSONObject inputObj = new JSONObject();

        //Add all the Inputs
        inputObj.put("ids", id);
        inputObj.put("name", name);

        //we need add the comments as a List

        JSONObject commentObj = new JSONObject();

        commentObj.put("comment", CommentToUpdate);


        SONArray list = new JSONArray();
        list.add(commentObj); 

        inputObj.put("comment", list);

        Client client = ClientBuilder.newClient()

        WebTarget base = client.target(url);

        //But I am unable to figureout how I can send the above JSON object to the 
        //put method

        Response response = base.request().put();

    }

如何將輸入發送到put方法? 這方面的任何指示都將有所幫助

您可以嘗試以下方法:

Response response = base.request().put(Entity.json(inputObj.toJSONString()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM