gpt4 book ai didi

java - 需要 Jersey 客户端 api 方式来发布带有 json 有效负载和 header 的网络请求

转载 作者:行者123 更新时间:2023-11-29 06:36:05 26 4
gpt4 key购买 nike

我正在使用 jersey(org.glassfish.jersey.client.*) 为我的一个 REST API 编写客户端。

api url 是:http://localhost:5676/searchws/search/getresults (POST)

此 api 返回一个 json 响应。我需要使用 Jersey 客户端提供有效载荷,这就是我被困的地方。以下是我需要提供的有效负载的样本提取(最好是字符串)

问题是我如何将有效负载 (XML/JSON) 作为字符串或实体提供给我的 webtarget。

我看到了calden提到的providing payload的答案How to send Request payload to REST API in java?但我正在寻找一种在 Jersey 客户中做到这一点的方法。

到目前为止,这是我的代码,它不能完全用于发布请求。

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

}

最佳答案

您将有效负载指定为 Entity.json 的参数

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

关于java - 需要 Jersey 客户端 api 方式来发布带有 json 有效负载和 header 的网络请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20493395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com