java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > RestTemplate使用:设置请求头、请求体

RestTemplate使用之如何设置请求头、请求体

作者:Hello_xzy_Word

这篇文章主要介绍了RestTemplate使用之如何设置请求头、请求体问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

RestTemplate使用:设置请求头、请求体

HttpEntity ⭐

使用 RestTemplate 时可以通过 HttpEntity 设置请求头和请求体。HttpEntity 有4个构造方法:

HttpEntity 源码:

/**
 * Create a new, empty {@code HttpEntity}.
 */
protected HttpEntity() {
  this(null, null);
}
/**
 * Create a new {@code HttpEntity} with the given body and no headers.
 * @param body the entity body
 */
public HttpEntity(T body) { // 只设置请求体
  this(body, null);
}
/**
 * Create a new {@code HttpEntity} with the given headers and no body.
 * @param headers the entity headers
 */
public HttpEntity(MultiValueMap<String, String> headers) { // 只设置请求头
  this(null, headers);
}
/**
 * Create a new {@code HttpEntity} with the given body and headers.
 * @param body the entity body
 * @param headers the entity headers
 */
public HttpEntity(T body, MultiValueMap<String, String> headers) { // 同时设置请求体与请求头
  this.body = body;
  HttpHeaders tempHeaders = new HttpHeaders();
  if (headers != null) {
      tempHeaders.putAll(headers);
  }
  this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
}

1、为 post、put 请求设置请求头、请求体

如果是为 post、put 请求设置请求头、请求体,可以在调用方法时,利用第二个参数传入 HttpEntity 对象,例如:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyRequestHeader", "MyValue");
HttpEntity requestEntity = new HttpEntity(requestHeaders);
Book book = restTemplate.postForObject("http://127.0.0.1:8080/getbook", requestEntity, Book.class);

PS:public class HttpHeaders implements MultiValueMap<String, String>, Serializable

同时设置请求头和请求体:

    @PostMapping("post_with_body_and_header")
    public void postWithBodyAndHeader(@RequestBody(required = false) UserEntity requestBody) {
        // 1.请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("headerName1", "headerValue1");
        httpHeaders.add("headerName2", "headerValue2");
        httpHeaders.add("headerName3", "headerValue3");
        httpHeaders.add("Content-Type", "application/json"); // 传递请求体时必须设置
        // 2.请求头 & 请求体
        HttpEntity<String> fromEntity = new HttpEntity<>(JSONUtil.toJsonStr(requestBody), httpHeaders);
        MessageBox responseBody = restTemplate.postForObject(INVOKE_URL + "/test/receive", fromEntity, MessageBox.class);
        log.info("响应体:{}", JSONUtil.toJsonPrettyStr(responseBody));
    }

2、为其他请求设置请求头、请求体

如果是其它HTTP方法调用要设置请求头,可以使用exchange()方法:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyRequestHeader", "MyValue");
HttpEntity requestEntity = new HttpEntity(requestHeaders);
HttpEntity<String> response = template.exchange(
        "http://example.com/hotels/{hotel}",
        HttpMethod.GET, 
        requestEntity, 
        String.class, 
        "42"
    );
String responseHeader = response.getHeaders().getFirst("MyResponseHeader");
String body = response.getBody();

RestTemplate添加请求头

//请求添加token头
HttpHeaders headers = new HttpHeaders();
headers.add("token",token);
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
ResponseEntity<String> resEntity = restTemplate.exchange(url.toString(), HttpMethod.GET, requestEntity, String.class);
//请求结果
String str = resEntity.getBody();

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文