Java 需要 Jersey 客户端 api 方式来发布带有 json 有效负载和标头的 web 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20493395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need Jersey client api way for posting a webrequest with json payload and headers
提问by Ashish Bajpai
I am writing a client for one of my REST API using jersey(org.glassfish.jersey.client.*).
我正在使用 jersey(org.glassfish.jersey.client.*) 为我的 REST API 之一编写客户端。
api url is : http://localhost:5676/searchws/search/getresults
(POST)
api 网址是:http://localhost:5676/searchws/search/getresults
(POST)
this api returns a json response. i need to provide a payload using jersey client and thats where i am stuck. FOllowing is a sample extract of payload which i need to provide (preferably as string)
这个 api 返回一个 json 响应。我需要使用 jersey 客户端提供有效负载,这就是我被卡住的地方。以下是我需要提供的有效载荷的示例提取物(最好是字符串)
Question is how can i provide a payload (XML/JSON) as string or entity to my webtarget.
问题是我如何将有效负载(XML/JSON)作为字符串或实体提供给我的 webtarget。
I saw the answer to providing payload mentioned by calden How to send Request payload to REST API in java?but i am looking for a way to do it in jersey client.
我看到了 calden 提到的提供有效负载的答案How to send Request payload to REST API in java? 但我正在寻找一种在球衣客户端中做到这一点的方法。
Here is my code till now which does not work fully for post requests.
这是我到目前为止的代码,它不能完全用于发布请求。
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);
}
}
采纳答案by Ashish Bajpai
I got this working using following code, Salil's code works fine as well(+1 with thanks to him), thanks everyone who contributed to this problem, loving stackoverflow:
我使用以下代码完成了这项工作,Salil 的代码也运行良好(感谢他+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));
}
}
回答by Salil
You specify payload as the argument to Entity.json
您将有效负载指定为 Entity.json 的参数
String payload = "{\"query\":\"(filter:(\\"google\\")) AND (count_options_availbale:[1 TO *])\"}";
response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(payload));