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