RestTemplate发送请求并携带header信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RestTemplate发送请求并携带header信息相关的知识,希望对你有一定的参考价值。

1、使用restTemplate的postForObject方法

  注:目前没有发现发送携带header信息的getForObject方法。

HttpHeaders headers = new HttpHeaders();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
  String key = (String) headerNames.nextElement();
  String value = request.getHeader(key);
  headers.add(key, value);
}        
String restStrResult = restTemplate.postForObject(url, /*携带header*/new HttpEntity<String>(headers), String.class,
             /*携带parameter*/request.getParameterMap());

2、使用restTemplate

import java.util.Enumeration;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class RestTemplateUtil {
    
    private RestTemplate restTemplate;
    
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String post(ServletRequest req, String url, Map<String, ?> params) {
        ResponseEntity<String> rss = request(req, url, HttpMethod.POST, params);
        return rss.getBody();
    }

    public String get(ServletRequest req, String url, Map<String, ?> params) {
        ResponseEntity<String> rss = request(req, url, HttpMethod.GET, params);
        return rss.getBody();
    }
    
    public String delete(ServletRequest req, String url, Map<String, ?> params) {
        ResponseEntity<String> rss = request(req, url, HttpMethod.DELETE, params);
        return rss.getBody();
    }
    
    public String put(ServletRequest req, String url, Map<String, ?> params) {
        ResponseEntity<String> rss = request(req, url, HttpMethod.PUT, params);
        return rss.getBody();
    }
    
    /**
     * @param req
     * @param url
     * @param method
     * @param params maybe null
     * @return
     */
    private ResponseEntity<String> request(ServletRequest req, String url, HttpMethod method, Map<String, ?> params) {
        HttpServletRequest request = (HttpServletRequest) req;
        //获取header信息
        HttpHeaders requestHeaders = new HttpHeaders();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
          String key = (String) headerNames.nextElement();
          String value = request.getHeader(key);
          requestHeaders.add(key, value);
        }
        //获取parameter信息
        if(params == null) {
            params = request.getParameterMap();
        }
        
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> rss = restTemplate.exchange(url, method, requestEntity, String.class, params);
        return rss;
    }
}

 

以上是关于RestTemplate发送请求并携带header信息的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D携带Header发送POST请求

vue2.0中axios发送跨域请求的时候如何设置header来携带token

spring RestTemplate发送请求返回值乱码问题解决心得

使用restTemplate发送带有身份验证标头的GET请求

使用RestTemplate发送multipart/form-data格式的数据

spring 5 webclient使用指南