1

jersey(org.glassfish.jersey.client.*) を使用して、REST API の 1 つのクライアントを作成しています。

API URL は: http://localhost:5676/searchws/search/getresults (POST)

この API は json レスポンスを返します。jersey クライアントを使用してペイロードを提供する必要があります。以下は、提供する必要があるペイロードのサンプル抽出です(できれば文字列として)

問題は、ペイロード (XML/JSON) を文字列またはエンティティとして webtarget に提供する方法です。

calden How to send Request payload to REST API in java? で言及されているペイロードを提供するための答えを見ました。しかし、ジャージークライアントでそれを行う方法を探しています。

これまでの私のコードは、投稿リクエストに対して完全には機能しません。

public class RequestGenerator 
{

    private WebTarget target;
    private ClientConfig config;
    private Client client;
    private Response response;

    public RequestGenerator(Method RequestSendingMethod) throws Exception
    {
        switch (RequestSendingMethod)
        {
            case POST :
                config = new ClientConfig();
                client = ClientBuilder.newClient(config);
                target = client.target("http://localhost:5676/searchws").path("search").path("getresults");
                String payload = "{\"query\":\"(filter:(\\\"google\\\")) AND (count_options_availbale:[1 TO *])\"}"; //This is just a sample json payload actual one is pretty large
                response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json("")); // What to do here
                String jsonLine = response.readEntity(String.class);
                System.out.println(jsonLine);
}

}
4

2 に答える 2

5

Entity.json への引数としてペイロードを指定します

 String payload = "{\"query\":\"(filter:(\\\"google\\\")) AND (count_options_availbale:[1 TO *])\"}";  
 response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(payload));
于 2013-12-11T06:58:37.757 に答える