博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RestTemplate发送请求并携带header信息
阅读量:6240 次
发布时间:2019-06-22

本文共 4542 字,大约阅读时间需要 15 分钟。

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;   }
你可能感兴趣的文章
2017UGUI之slider
查看>>
python下载酷狗音乐源码
查看>>
MySQL学习----explain查看一条sql 的性能
查看>>
第零次作业
查看>>
Android + eclipse +ADT安装完全教程
查看>>
【批处理学习笔记】第七课:简单的批处理命令(6)
查看>>
leetcode 【 Subsets 】python 实现
查看>>
leetcode 【 Intersection of Two Linked Lists 】python 实现
查看>>
codeforces 767A Snacktower(模拟)
查看>>
用 Quartz 画聊天对话框背景实例
查看>>
Quartz2D简单绘制之饼状图
查看>>
你优化系统的目标是什么?
查看>>
SVN(64位)报 Failed to load JavaHL Library. 的解决方法
查看>>
基本运算符
查看>>
黄聪:WordPress 多站点建站教程(三):主站如何调用子站的文章内容、SQL语句如何写?...
查看>>
Activity的启动模式 4种launchMode Intent.FLAG_NEW_TASK 详解
查看>>
hdu 2254 奥运 **
查看>>
数据结构基础
查看>>
UltraISO制作ISO镜像文件
查看>>
ASP.NET MVC 之自定义HtmlHelper
查看>>