gpt4 book ai didi

java - 如何使用 jersey 将 header 信息作为键值对传递以使用休息服务

转载 作者:行者123 更新时间:2023-12-01 09:10:47 25 4
gpt4 key购买 nike

我得到了一个 Web 服务的 url,它返回 json 格式的值,但它需要 get 请求中的 header 信息作为键值对,例如我需要传递 Emp_code 作为键,将“xyz”作为值传递,以获取 postman 中所有员工的详细信息。下面是我尝试过的代码

private static void getEmployees()
{
final Client client = new Client();
final WebResource webResource = client.resource("http://abc/springrestexample/employees");
final ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
if (response.getStatus() != 200)
{
throw new RuntimeException("Failed Http Error code " + response.getStatus());
}
final String output = response.getEntity(String.class);

System.out.println("Output from Server .... \n");
System.out.println(output);
}

在上面的代码中,我应该如何传递 header 信息(键值)以获得所需的结果。

最佳答案

您可以在接受后添加 .header("KEY", "Value")。请检查下面

final Client client = new Client();
final WebResource webResource = client.resource("http://abc/springrestexample/employees");
final ClientResponse response = webResource.accept("application/json").header("KEY", "Value" ).get(ClientResponse.class);
if (response.getStatus() != 200)
{
throw new RuntimeException("Failed Http Error code " + response.getStatus());
}
final String output = response.getEntity(String.class);

System.out.println("Output from Server .... \n");
System.out.println(output);

关于java - 如何使用 jersey 将 header 信息作为键值对传递以使用休息服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40934274/

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