使用restTemplate发送带header的get请求

  • 首先贴一段网友的代码:
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

HttpHeaders headers = new HttpHeaders();
headers.add("token", "123");
ResponseEntity response = restTemplate.exchange(rootUrl + "get4", HttpMethod.GET, new HttpEntity(headers), String.class);
System.out.println("设置header的Get请求:" + response.getBody());
  • 改造,使其适用我自己的需求,需求是 调用harbor的api,获取镜像列表。这需要使用header传递账户信息,使其有权限访问harbor。

实体类如下:


/**
 * @Auther: 
 * @Date: 2020/5/20
 * @Description:
 */

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPro

你可能感兴趣的:(和彡十一起学java)