RestTemplate发送请求并携带header信息

http://www.cnblogs.com/hujunzheng/p/6018505.html

****************************

v1、使用restTemplate的postForObject方法

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

复制代码
HttpHeaders headers = new HttpHeaders();
Enumeration 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(headers), String.class,
             /*携带parameter*/request.getParameterMap());
复制代码

v2、使用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 params) {
        ResponseEntity rss = request(req, url, HttpMethod.POST, params);
        return rss.getBody();
    }

    public String get(ServletRequest req, String url, Map params) {
        ResponseEntity rss = request(req, url, HttpMethod.GET, params);
        return rss.getBody();
    }
    
    public String delete(ServletRequest req, String url, Map params) {
        ResponseEntity rss = request(req, url, HttpMethod.DELETE, params);
        return rss.getBody();
    }
    
    public String put(ServletRequest req, String url, Map params) {
        ResponseEntity rss = request(req, url, HttpMethod.PUT, params);
        return rss.getBody();
    }
    
    /**
     * @param req
     * @param url
     * @param method
     * @param params maybe null
     * @return
     */
    private ResponseEntity request(ServletRequest req, String url, HttpMethod method, Map params) {
        HttpServletRequest request = (HttpServletRequest) req;
        //获取header信息
        HttpHeaders requestHeaders = new HttpHeaders();
        Enumeration 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 requestEntity = new HttpEntity(null, requestHeaders);
        ResponseEntity rss = restTemplate.exchange(url, method, requestEntity, String.class, params);
        return rss;
    }
}
复制代码

v 3.restTemplate以json格式发送post请求

  1.发送请求

复制代码
  public String _post(ServletRequest req, String url, Map params) {
        HttpServletRequest request = (HttpServletRequest) req;
        //拿到header信息
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
          String key = (String) headerNames.nextElement();
          String value = request.getHeader(key);
          requestHeaders.add(key, value);
        }
        net.sf.json.JSONObject jsonParams = net.sf.json.JSONObject.fromObject(params);
        HttpEntity requestEntity = new HttpEntity(jsonParams, requestHeaders);
        String result = restTemplate.postForObject(url, requestEntity, String.class);
        return result;
    }
复制代码

  2.接收处理

复制代码
   @RequestMapping("/xxx")  
    @ResponseBody  
    public String xxx(HttpServletRequest request) throws IOException {  
  
        String jsonString = getBodyString(request.getReader());  
  
        JSONObject jbJsonObject = new JSONObject().fromObject(jsonString);  
  
        User user = (User) JSONObject.toBean(jbJsonObject, User.class);  
        System.out.println(jbJsonObject);  
        System.out.println("id:" + user.getUserName());return null;  
  
    }  
  
    @RequestMapping("/xxx2")  
    @ResponseBody  
    public String xxx2(User user) throws IOException {  
  
        System.out.println("---------------");  
        System.out.println(user.getUserName());  
        System.out.println(user.getPassWord());  
        System.out.println("---------------");  
  
        if (true) {  
            return "success";  
        } else {  
            return "fail";  
        }  
  
    }  

  
  public String getBodyString(BufferedReader br) { 
    String inputLine; 
    String str = ""; 
    try { 
      while ((inputLine = br.readLine()) != null) { 
      str += inputLine; 
    } 
      br.close(); 
    } catch (IOException e) { 
      System.out.println("IOException: " + e); 
    } 
    return str; 
  }


你可能感兴趣的:(spring)