RestTemplate 发送post请求
springboot使用restTemplate post提交值 restTemplate post值
post提交有 FormData和Payload 两种形式:
第一种是formdata形式,在header参数里可以直接看到
payload则封装成json格式post过去,获取以后需要再解析成实体。
restTemplate post json格式
使用阿里巴巴的json包 com.alibaba.fastjson
代码demo如下:
url='http://posturl';JSONObject postData = new JSONObject();
postData.put("shopid", 1);
JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody();
如果要使用post formdata形式则需要
使用RestTemplate发送multipart/form-data格式的数据
复制代码
String url = 'http://posturl';
MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("shopid","1");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
return restTemplate.postForEntity(url, request,String.class);
复制代码
对header进行请求头设置,如果不设置也可以直接post那么就是如下的
复制代码
String url = 'http://posturl';
MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("shopid","1");
return restTemplate.postForEntity(url, map,String.class);
RestTemplate 发送post请求的更多相关文章
- 使用RestTemplate发送post请求
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
- 使用RestTemplate发送post请求,请求头中封装参数
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败.中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
- RestTemplate 发送 get 请求使用误区 多个参数传值为null(转载)
首先看一下官方文档是怎么描述的,传递多个值的情况(注意例子中用到的@pathParam,一般要用@queryParam) RestTemplate 实例 @Configuration public c ...
- RestTemplate 发送 get 请求使用误区 多值为null
http://blog.csdn.net/zhousenshan/article/details/71055687 ****************************************** ...
- 使用restTemplate发送post请求,传入参数是在requestBody请求体中,以json形式传输
@PostMapping public ResponseResult add(User user){ HttpHeaders httpHeaders = new HttpHeaders(); Medi ...
- RestTemplate发送GET请求
import org.springframework.web.client.RestTemplate; @Component @Slf4j public class JsSdkUtil { /** * ...
- RestTemplate发送请求并携带header信息
1.使用restTemplate的postForObject方法 注:目前没有发现发送携带header信息的getForObject方法. HttpHeaders headers = new Http ...
- RestTemplate发送请求并携带header信息 RestTemplate post json格式带header信息
原文地址: http://www.cnblogs.com/hujunzheng/p/6018505.html RestTemplate发送请求并携带header信息 v1.使用restTempl ...
- RestTemplate发送HTTP、HTTPS请求
RestTemplate 使用总结 场景: 认证服务器需要有个 http client 把前端发来的请求转发到 backend service, 然后把 backend service 的结果再返 ...
随机推荐
- python中导入sklearn中模块提示ImportError: DLL load failed: 找不到指定的程序。
python版本:3.7 平台:windows 10 集成环境:Anaconda3.7 64位 在jupyter notebook中导入sklearn的相关模块提示ImportError: DLL l ...
- LongAccumulator 源码分析
LongAccumulator LongAccumulator 能解决什么问题?什么时候使用 LongAccumulator? 1)LongAccumulator 的逻辑和 LongAdder 基本类 ...
- Linux_NIS+NFS+Autofs
目录 目录 前言 NIS NFS Autofs 搭建NISNFSAutofs Setup NNA environment Setup ServerSite Setup client 前言 NIS+NF ...
- 封装redis(set/get/delete)str和哈希类型
将Redis的常用操作封装了一下: import redis class MyRedis(): def __init__(self,ip,passwd,port=6379,db=0): #构造函数 t ...
- Matplotlib字体大小设置
参考:https://blog.csdn.net/henkekao/article/details/72871882 ax = plt.subplot(111) # 设置刻度字体大小 plt.xtic ...
- vue中淡入淡出示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【MM系列】SAP MM模块-基础配置第一篇
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-基础配置第一篇 ...
- Spring002--实现读写分离(Mysql实现主从复制)
Spring AOP实现读写分离(Mysql实现主从复制) 本文来自于博客:http://www.cnblogs.com/bjlhx/p/8297460.html 一.背景 一般应用对数据库而言都是“ ...
- WPF使用Mutex创建单实例程序失效
vs2019 1.引入名称空间 using System.Threading; using System.Runtime.InteropServices; 2.导入dll并声明方法 [DllImpor ...
- [Web 前端] 004 html 小练习
1. 锚点 用法 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...