【问题标题】:How to pass header values using RestTemplate?如何使用 RestTemplate 传递标头值?
【发布时间】:2023-03-21 08:41:01
【问题描述】:

我是 Spring 新手。我的一个项目需要调用一个 Autotask API,它在标题中采用 Api 集成代码、用户名和密码(密码)。如何将其集成到 Rest 模板中?

下面是我在 swagger 上测试的 API 的屏幕截图。 Screenshot of API Call using Swagger

【问题讨论】:

    标签: spring spring-boot swagger resttemplate


    【解决方案1】:

    您需要发送的附加参数都是标头。您可以按如下方式设置标题:

    HttpHeaders headers = new HttpHeaders();
    headers.set("ApiIntegrationCode", "HUCXSL...");
    headers.set("UserName", "fdfsk...");
    headers.set("Secret", "yR*42...");
    

    然后将标头添加到新的HttpEntity 实例并使用RestTemplate 执行请求:

    HttpEntity entity = new HttpEntity(headers);
    
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
    

    这里提出了一个非常相似的问题:HTTP get with headers using RestTemplate

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多