简介:RestTemplate是Spring提供的HTTP请求调用工具
导入包:org.springframework.web.client.RestTemplate

三种postForObject方法
postForObject(URI url, Object request, Class responseType): T - RestTemplate
postForObject(String url, Object request, Class responseType, Map< String,?> uriVariables): T - RestTemplate
postForObject(String url, Object request, Class responseType, Object… uriVariables): T - RestTemplate

参数含义
String url 请求的URL路径
Object request 请求的body,通过@RequestBody接收传过去的值
Class responseType 请求完成之后返回的结果类型
Map< String,?> uriVariables POST请求中要传过去的参数
Object… uriVariables 可以放零个或多个Object类型的参数

使用
使用Map传值

import org.springframework.web.client.RestTemplate
@Service
public class DemoServiceImpl implements DemoService {@Overridepublic void send(){Map<String, String> input = new HashMap<String, String>();RestTemplate restTemplate = new RestTemplate();String url = "http://xxx/api/send.do"input.put("username", "admin");input.put("mobile", "13312340066");String sendResultStr = restTemplate.postForObject(url+"?username={username}&mobile={mobile}", null, String.class, input);}
}

使用可变长参数传值

import org.springframework.web.client.RestTemplate
@Service
public class DemoServiceImpl implements DemoService {@Overridepublic void send(){Map<String, String> input = new HashMap<String, String>();RestTemplate restTemplate = new RestTemplate();String url = "http://xxx/api/send.do"String username = "admin";String mobile = "13312340066";String sendResultStr = restTemplate.postForObject(url+"?username={username}&mobile={mobile}", null, String.class, username, mobile);}
}

使用request传值

import org.springframework.web.client.RestTemplate
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@Service
public class DemoServiceImpl implements DemoService {@Overridepublic void send(){Map<String, String> input = new HashMap<String, String>();RestTemplate restTemplate = new RestTemplate();String url = "http://xxx/api/send.do"MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();paramMap.add("username", "admin");paramMap.add("mobile", "13312340066");String sendResultStr = restTemplate.postForObject(url, paramMap, String.class);}
}

接收值

@Controller
public class DemoController {@PostMapping("/api/send.do")public String home(String username, String mobile){return "success";}
}

restTemplate.postForObject相关推荐

  1. restTemplate.postForObject 发送POST请求

    1.引用依赖文档: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  2. java get resttemplate 请求传递数组_RestTemplate入门

    RestTemplate入门  本篇主要讲解RestTemplate的基本使用,它是Spring提供的用来访问Rest服务的客户端,RestTmplate提供了很多便捷的方法,可以大大提供开发效率,本 ...

  3. Spring boot ----RestTemplate学习笔记

    ****spring boot-----restTemplate 封装了HttpURLConnection,HttpClient,Netty等接口访问实现库 restTemplet包含以下部分 Htt ...

  4. SpringBoot系列: RestTemplate 快速入门

    ==================================== 相关的文章 ==================================== SpringBoot系列: 与Sprin ...

  5. 拦截器获取请求参数post_「SpringBoot WEB 系列」RestTemplate 之自定义请求头

    [WEB 系列]RestTemplate 之自定义请求头 上一篇介绍了 RestTemplate 的基本使用姿势,在文末提出了一些扩展的高级使用姿势,本篇将主要集中在如何携带自定义的请求头,如设置 U ...

  6. 4.3. postForObject

    4.3.1. 传递对象 @RequestMapping("/restful/post/{id}")@ResponseBodyprivate static String restfu ...

  7. Spring RESTFul Client – RestTemplate Example--转载

    原文地址:http://howtodoinjava.com/2015/02/20/spring-restful-client-resttemplate-example/ After learning ...

  8. easypoi教程_SpringBoot图文教程17—上手就会 RestTemplate 使用指南

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1-Spr ...

  9. java web乱码_【SpringBoot WEB 系列】RestTemplate 之中文乱码问题 fix

    [WEB 系列]RestTemplate 之中文乱码问题 fix 在 RestTemplate 基础用法博文中,post json 表单时,会发现存在中文乱码问题,本文主要介绍对应的解决方案 I. 中 ...

最新文章

  1. Bash中的逻辑运算
  2. revit导出lumion插件_建筑工程BIM建模入门级教程——REVIT的几种渲染方式
  3. python函数type的用意_python之type函数
  4. form 中Enctype=multipart/form-data 的作用
  5. 生成模型和判别模型_生成模型和判别模型简介
  6. bitmap 转byte[]后读取_闲谈redis的bitmap
  7. HTML--HTML对象的关于位置和大小的属性的图解
  8. 13新功能_再聊聊灵感盒 -Marginnote 3.6.12/13新功能
  9. Java笔记-解决SSLHandshakeException: No subject alternative names present
  10. XML具有哪些特点?相对于HTML的优势
  11. Unity中的单例方法
  12. Maria DB windows 安装
  13. android 读取图片字节流,Android屏幕截图直接读取screencap流来实现
  14. MSN Message协议分析
  15. 全局快门与卷帘式快门
  16. Sparkling Logic决策引擎操作手册
  17. 诗词格律[7] 诗词的唱和
  18. Detected applied migration not resolved locally:
  19. 阿基米德螺旋线lisp_CAD画阿基米德螺旋线程序
  20. 获取晋江优质小说(按章节数量选择、python多进程)

热门文章

  1. Spring原理以及流程
  2. NFT项目成功的底层逻辑是什么?非蓝筹NFT项目怎么破局?
  3. iPhone11和华为P40Pro哪个好
  4. Lodash系列之 merge 函数
  5. 聚观早报 |必应成为中国第一大桌面搜索引擎;快手上市后首次盈利
  6. 写给像我一样初次接触渗透测试的人
  7. python csv文件使用excel打开数字丢失精度
  8. 常用Cron表达式整理
  9. python爬取晋江小说简介_python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇...
  10. 学大数据要学哪些算法_大数据分析都有哪些常见的算法