gpt4 book ai didi

java - 如何解决 'HttpHeaders' has private access in 'org.apache.http.HttpHeaders' 错误

转载 作者:行者123 更新时间:2023-12-02 08:29:48 31 4
gpt4 key购买 nike

我正在尝试使用 RestTemplate 发出 Http 请求,但它一直给我错误:'HttpHeaders' 在 'org.apache.http.HttpHeaders' 中具有私有(private)访问权限

我只是想写这一行:

HttpHeaders headers = new HttpHeaders();

最佳答案

包名错误,为了在使用Spring RestTemplate时添加头,应该使用org.springframework.http.HttpHeaders.HttpHeaders而不是org.apache.http.HttpHeaders 。以下是添加请求 header 的代码片段。

// request resource
HttpHeaders headers = new HttpHeaders();
headers.set("headerName", "headerValue");

HttpEntity entity = new HttpEntity(headers);

ResponseEntity<String> response = restTemplate.exchange("https://example.com", HttpMethod.GET, entity, String.class);

关于java - 如何解决 'HttpHeaders' has private access in 'org.apache.http.HttpHeaders' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50951583/

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