2013-12-10 3 views
1

jersey (org.glassfish.jersey.client. *)를 사용하여 REST API 중 하나의 클라이언트를 작성하고 있습니다.json 페이로드와 헤더로 웹 요청을 게시하기 위해 Jersey 클라이언트 API가 필요합니다.

API URL은 다음과 같습니다 http://localhost:5676/searchws/search/getresults (POST)

이 API는 JSON 응답을 반환합니다. 저지 클라이언트를 사용하여 페이로드를 제공해야하고 내가 멈추는 곳이 필요합니다. FOllowing은 내가 제공해야하는 페이로드의 샘플 추출물입니다 (문자열로 설정하는 것이 좋습니다)

질문 웹 사이트에 문자열 또는 엔터티로 페이로드 (XML/JSON)를 제공하려면 어떻게해야합니까?

나는 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); 
} 

} 

답변

3

을 Entity.json하기 위해 인수로 페이로드를 지정 나는이 작업을 사용하여 다음 코드는, 살릴의 코드는, 모두 감사합니다 (그 덕분에 +1)뿐만 아니라 잘 작동 가지고있는 사람 이 문제에 기여, 사랑의 stackoverflow :

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 :    
        String payload = "\r\n{\r\n\"query\": \"google \",\r\n\"rows\": 50,\r\n\"return_docs\": true,\r\n\"is_facet\": true\r\n}"; //this is escapped json string in single line 
        config = new ClientConfig(); 
        client = ClientBuilder.newClient(config); 
        target = client.target("http://localhost:7400/searchws/search/getresults"); 
        response = target.request().accept(MediaType.APPLICATION_JSON).post(Entity.entity(payload, MediaType.APPLICATION_JSON), Response.class); 
        processresponse(response); //This could be any method which processes your json response and gets you your desired data. 
        System.out.println(response.readEntity(String.class)); 
        break; 
       case GET : 
        config = new ClientConfig(); 
        client = ClientBuilder.newClient(config); 
        target = client.target("http://localhost:7400/search-service/searchservice").path("search").path("results").path("tiger"); 
        response = target.request().accept(MediaType.APPLICATION_JSON).get(); 
        processresponse(response); //This could be any method which processes your json response and gets you your desired data. 
        System.out.println(response.readEntity(String.class)); 
      } 

} 
4

당신은

String payload = "{\"query\":\"(filter:(\\\"google\\\")) AND (count_options_availbale:[1 TO *])\"}"; 
response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(payload)); 
+1

감사합니다 살릴이 작동합니다. 나는 또한 다음 코드 응답 = target.request()를 사용하여이 작업을 얻었다. (MediaType.APPLICATION_JSON) .post (Entity.entity (payload, MediaType.APPLICATION_JSON), Response.class); –