RestTemplate的超全讲解(全)转

https://blog.csdn.net/weixin_47872288/article/details/121842374

(81条消息) RestTemplate的超全讲解(全)_码农研究僧的博客-CSDN博客_resttemplate

视频地址:

https://www.bilibili.com/video/BV1tJ41147uj?t=624.5&p=4

post与get的区别在于post的方法传参map必须是MultiValueMap

基本类型传参和实体类型传参

@RestController
public class UserController{
//实体传参
@RequestMapping(value="/addUser1")
public UserDTO addUser(userDTO userdto)
{
return userdto;
} @ResquestMapping(value="/addUser2")
public UserDTO addUser2(Long userId,String userName)
{
UserDTO u=new UserDTO();
u.setUserId(userId);
u.setUserName(userName);
return u;
} }

Post 请求方式调用服务接口

//http://localhost:8080/postFor0bject1
@GetMapping("/postForObject1")
public UserDT0 postForObject1 ( ) {
//远程访问的Url UserDTO
String url = "http://localhost:8080/addUser1";
// Post方法必须使用MultiValueMap传参。//使用UserDTO传参也可以 MultiValueMap<String,0bject> paramMap = new LinkedMultiValueMap<>();
paramMap.add ("userId", 1008L);
paramMap.add ("userName ","巧克力");
UserDT0 userDTO = restTemplate.postForObject(url,paramMap,UserDT0.class);
return userDTO;
}

如果服务接口使用到@RequestBody,用httpentity  ,此时不能使用MultiValueMap 传参!!

@RequestMapping( value = "/addUser3" )
public UserDT0 addUser3(@RequestBody UserDT0 userDTO) {
userDTo.setUserName(userDTo.getUserName() + " from RequestBody" );
return userDTO;
}

//http://localhost:8888/postForObject2
@GetMapping("/postForObject2")
public UserDT0 postForObject2( ) {
//申明一个请求头
HttpHeaders headers = new HttpHeaders();
//application/json
headers.setContentType( MediaType .APPLICATION_JSON);//远程访问的Url UserDTO
string url = "http://localhost:8080/addUser3";
/**
此处使用MultiValueMap会报错
MultiValueMap<String,0bject> paramMap = new LinkedMultiValueMap<>( );
paramMap.add("userId",100OL) ;
paramMap.add("userName","fencaibc");*/
//此处可以使用HashMap代替,但是会有警告 UserDT0 userDTO = new UserDTO( );
userDTO.setUserId( 1088L);
userDT0.setUserName("课程");
HttpEntity<UserDTO> entityParam new HttpEntity<UserDTO>(userDTO,headers) ;
UserDT0 result = restTemplate.postFor0bject(url, entityParam,UserDTO.class);
return result ;
}

PostForEntity 方法 返回内容比 postForObject 更丰富,但是要注意,基本类型的参数采用 MultiValueMap封装参数,@ReqeustBody 修饰的参数 要以httpEntity 封装对象实例作为参数

同时,返回结果要是 ResponseEntity类型

//http://localhost:8088/ postForEntity1@GetMapping( " / postForEntity1")
public UserDT0 postForEntity1( ) {
string url = "http://localhost:8080/addUser1" ; MultiValueMap<String,object> paramMap = new LinkedMultiValueMap<>( ); paramMap.add("userId", 100);
paramMap.add("userName" ,"课程"); ResponseEntity<UserDT0> userDTOResponseEntity =restTemplate.postForEntity(url,paramMap,UserDTO.class) ; HttpStatus statusCode = userDTOResponseEntity.getStatusCode( ); int statusCodeValue = userDTOResponseEntity.getStatusCodeValue( ); HttpHeaders headers = userDTOResponseEntity. getHeaders( ); return userDTOResponseEntity. getBody( );
}

效果

RestTemplate的超全讲解(全)转的更多相关文章

  1. 一行导出所有任意微软SQL server数据脚本-基于Python的微软官方mssql-scripter工具使用全讲解

    文章标题: 一行导出所有任意微软SQL serer数据脚本-基于Python的微软官方mssql-scripter工具使用全讲解 关键字 : mssql-scripter,SQL Server 文章分 ...

  2. 在jquery中,全选/全不选的表示方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  3. JS checkbox 全选 全不选

    /* JS checkbox 全选 全不选 Html中checkbox: <input type="checkbox" name="cbx" value= ...

  4. checkbox实现全选全不选

    1.jQuery实现checkbox全选全不选 <!DOCTYPE html> <head runat="server"> <title>jQu ...

  5. jquery 全选 全不选 反选

    1.概述 在项目中经常遇到列表中对复选框进行勾选操作,全选...反选.. 2. example <html> <body> <form id="test-for ...

  6. 利用jQuery实现CheckBox全选/全不选/反选

    转自:http://www.cnblogs.com/linjiqin/p/3148259.html jQuery有些版本中实现CheckBox全选/全不选/反选会有bug,经测试jquery-1.3. ...

  7. jquery的全选/全不选/反选以及attr添加checked属性失败的解决办法

    如下图: <head> <title></title> <style type="text/css"> div { border: ...

  8. Jq 遍历 全选 全不选 反选

    //全选 全不选 $('#checkAll').click(function () { //判断是否被选中 var bischecked = $('#checkAll').is(':checked') ...

  9. js之checkbox的代码全选/全不选,使用id获取元素,而不是name

    每当有多个选项的时候,都会有一种想法是:全选,全不选,如果子选项有被选,父选项也得被选. 注意:这里是根据id来获取元素的,但是不能直接用getElementById,因为那只能返回一个,而不是集合. ...

  10. jquery之全选全不选

    <input type="checkbox" onclick="selall(this)" />全选/全不选 <input type=&quo ...

随机推荐

  1. 前端面试HTML和CSS总结,这一篇就够了!

    一,面试基础 HTML和CSS ps:这俩面试答不上来的,基本就可以回去了,以下是HTML题,一般来说这地方不会出太多题,面试官也不愿意花太多时间在这上面. 1,HTML语义化,如何理解语义化? 让人 ...

  2. 动态规划篇——线性DP

    动态规划篇--线性DP 本次我们介绍动态规划篇的线性DP,我们会从下面几个角度来介绍: 数字三角形 最长上升子序列I 最长上升子序列II 最长公共子序列 最短编辑距离 数字三角形 我们首先介绍一下题目 ...

  3. 解决can't compare offset-naive and offset-aware datetimes报错

    问题描述 在比较 <class 'datetime.datetime'> 类型时,抛出异常 原因 俩个做比较的,一个具有时区,一个不具有时区 解决 如果可以确认俩个时间都是本地时间可以将时 ...

  4. java 常用的jar包下载地址

    Eclipse: http://www.eclipse.org/downloads/packages/all Spring: http://Framework: http://repo.spring. ...

  5. CheckBox 选中取值以及回填

    html: <td align="left" style="word-wrap:break-word;word-break:break-all;" col ...

  6. linux开机进入grub rescue界面修复

    一.先输入ls回车,查看显示内容 如: (hd0) (hd0,msdos1) (hd0,msdos2)... 二.然后找出哪个盘安装了系统 ls (hd0,msdos1)/boot/grub/ ls( ...

  7. 【每日一题】2022年2月10日-NC160 二分查找-I

    描述请实现无重复数字的升序数组的二分查找 给定一个 元素升序的.无重复数字的整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标(下标 ...

  8. Scanner例题讲解

    Scanner例题讲解 题:输入多个平均数,求其总和与平均数;每输入一个数用回车确认,通过输入非数字来结束输入并输出执行结果  public class Demo05 {     //输入多个平均数, ...

  9. 【leetcode】剑指offer04二维数组查找

    很巧妙地把矩阵转化为二叉搜索树(不过好像没什用) class Solution { public: bool findNumberIn2DArray(vector<vector<int&g ...

  10. 把时间沉淀下来 | Kagol 的 2022 年终总结

    现代管理学之父德鲁克在其经典著作<卓有成效的管理者>中对时间有一段精妙的论述,其要点如下: 时间是一项限制因素,任何生产程序的产出量,都会受到最稀有资源的制约,而时间就是其中最稀有的资源. ...