restTemplate.postForObject
简介: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相关推荐
- restTemplate.postForObject 发送POST请求
1.引用依赖文档: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- java get resttemplate 请求传递数组_RestTemplate入门
RestTemplate入门 本篇主要讲解RestTemplate的基本使用,它是Spring提供的用来访问Rest服务的客户端,RestTmplate提供了很多便捷的方法,可以大大提供开发效率,本 ...
- Spring boot ----RestTemplate学习笔记
****spring boot-----restTemplate 封装了HttpURLConnection,HttpClient,Netty等接口访问实现库 restTemplet包含以下部分 Htt ...
- SpringBoot系列: RestTemplate 快速入门
==================================== 相关的文章 ==================================== SpringBoot系列: 与Sprin ...
- 拦截器获取请求参数post_「SpringBoot WEB 系列」RestTemplate 之自定义请求头
[WEB 系列]RestTemplate 之自定义请求头 上一篇介绍了 RestTemplate 的基本使用姿势,在文末提出了一些扩展的高级使用姿势,本篇将主要集中在如何携带自定义的请求头,如设置 U ...
- 4.3. postForObject
4.3.1. 传递对象 @RequestMapping("/restful/post/{id}")@ResponseBodyprivate static String restfu ...
- Spring RESTFul Client – RestTemplate Example--转载
原文地址:http://howtodoinjava.com/2015/02/20/spring-restful-client-resttemplate-example/ After learning ...
- easypoi教程_SpringBoot图文教程17—上手就会 RestTemplate 使用指南
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1-Spr ...
- java web乱码_【SpringBoot WEB 系列】RestTemplate 之中文乱码问题 fix
[WEB 系列]RestTemplate 之中文乱码问题 fix 在 RestTemplate 基础用法博文中,post json 表单时,会发现存在中文乱码问题,本文主要介绍对应的解决方案 I. 中 ...
最新文章
- Bash中的逻辑运算
- revit导出lumion插件_建筑工程BIM建模入门级教程——REVIT的几种渲染方式
- python函数type的用意_python之type函数
- form 中Enctype=multipart/form-data 的作用
- 生成模型和判别模型_生成模型和判别模型简介
- bitmap 转byte[]后读取_闲谈redis的bitmap
- HTML--HTML对象的关于位置和大小的属性的图解
- 13新功能_再聊聊灵感盒 -Marginnote 3.6.12/13新功能
- Java笔记-解决SSLHandshakeException: No subject alternative names present
- XML具有哪些特点?相对于HTML的优势
- Unity中的单例方法
- Maria DB windows 安装
- android 读取图片字节流,Android屏幕截图直接读取screencap流来实现
- MSN Message协议分析
- 全局快门与卷帘式快门
- Sparkling Logic决策引擎操作手册
- 诗词格律[7] 诗词的唱和
- Detected applied migration not resolved locally:
- 阿基米德螺旋线lisp_CAD画阿基米德螺旋线程序
- 获取晋江优质小说(按章节数量选择、python多进程)