• RestTemplate发送请求并携带header信息


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

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

    v1、使用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());
    复制代码

    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<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;
        }
    }
    复制代码

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

      1.发送请求

    复制代码
      public String _post(ServletRequest req, String url, Map<String, ?> params) {
            HttpServletRequest request = (HttpServletRequest) req;
            //拿到header信息
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.setContentType(MediaType.APPLICATION_JSON);
            Enumeration<String> 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<net.sf.json.JSONObject> requestEntity = new HttpEntity<net.sf.json.JSONObject>(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; 
      }

  • 相关阅读:
    Java命名规范
    用U盘安装系统2
    将Sublime Text3添加到右键菜单中
    Git版本控制使用介绍
    Sublime Text各种插件使用方法
    安装Sublime Text 3插件
    Sublime Text修改显示图标
    卸载Visual Studio Code后删除右键Open with Code…
    做最好的自己(Be Your Personal Best)
    Eclipse全屏及插件下载
  • 原文地址:https://www.cnblogs.com/zhao1949/p/6339471.html
Copyright © 2020-2023  润新知