IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> 记录关于restemplate post方式传递的参数@RequestParam无法拿到的问题 -> 正文阅读

[开发工具]记录关于restemplate post方式传递的参数@RequestParam无法拿到的问题

自己遇到的问题,网上搜了很多都不是很靠谱。决定自己记录一下。

首先总结下来是 @RequestBody 和 @RequestParam在使用Conten-Type的时候有区别。

@RequestBody
处理HttpEntity传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据。
如果要用@RequestBody来接受restTemplate的参数,那么你在设置请求头的时候要用

headers.add(“Content-Type”,“application/json”);并且添加参数的集合可以是实体类,也可以是Hashmap.

@RequestMapping("test2")
    public Object test2(){
        //1、构建body参数
        RestTemplate restTemplate =new RestTemplate();
        Test test = new Test();  //Test是我自己创建的一个实体类  包含id和name两个属性
        test.setId("123");
        test.setName("name");

        //2、添加请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");

        //3、组装请求头和参数
        HttpEntity formEntity = new HttpEntity(test, headers);

        //4、发起post请求
        ResponseEntity<String> stringResponseEntity = null;
        String url = "http://localhost:8090/test/test1";
        try {
            stringResponseEntity = restTemplate.postForEntity(url, formEntity, String.class);
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        //5、获取http状态码
        int statusCodeValue = stringResponseEntity.getStatusCodeValue();

        //6、获取返回体
        String body = stringResponseEntity.getBody();

        //7、映射实体类
        Test wrapper = JSONObject.parseObject(body, Test.class);


        return wrapper;

    }

@RequestParam
用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)

如果要用@RequestParam来接受参数,那么设置请求头要用 headers.add(“Content-Type”,“application/x-www-form-urlencoded”);并且添加的参数集合要使用MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<String, String>();

@RequestMapping("test3")
    public Object test3(){
        //1、构建body参数
        RestTemplate restTemplate =new RestTemplate();
        MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<String, String>();
        paramMap.set("id","dddd");
        paramMap.set("name","fff");

        //2、添加请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/x-www-form-urlencoded");

        //3、组装请求头和参数
        HttpEntity formEntity = new HttpEntity(paramMap, headers);

        //4、发起post请求
        ResponseEntity<String> stringResponseEntity = null;
        String url = "http://localhost:8090/test/test";
        try {
            stringResponseEntity = restTemplate.postForEntity(url, formEntity, String.class);
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        //5、获取http状态码
        int statusCodeValue = stringResponseEntity.getStatusCodeValue();

        //6、获取返回体
        String body = stringResponseEntity.getBody();

        //7、映射实体类
        Test wrapper = JSONObject.parseObject(body, Test.class);


        return wrapper;

    }

总的测试代码如下

package com.example.tkmybatistest.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.tkmybatistest.entity.Test;
import com.example.tkmybatistest.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;



@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    TestService testService;

    @RequestMapping("/test")
    public Object test( @RequestParam("id") String id,@RequestParam("name") String name){
        
    Test test = new Test();
    test.setName("xuly");
    test.setId("123");

        return test;
    }

    @RequestMapping("/test1")
    public Object test1(@RequestBody Test test){

        Test test1 = new Test();
        test1.setName("test");
        test1.setId("zhangjun");

        return test1;
    }
    @RequestMapping("test2")
    public Object test2(){
        //1、构建body参数
        RestTemplate restTemplate =new RestTemplate();
        Test test = new Test();  //Test是我自己创建的一个实体类  包含id和name两个属性
        test.setId("123");
        test.setName("name");

        //2、添加请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");

        //3、组装请求头和参数
        HttpEntity formEntity = new HttpEntity(test, headers);

        //4、发起post请求
        ResponseEntity<String> stringResponseEntity = null;
        String url = "http://localhost:8090/test/test1" ;
        try {
            stringResponseEntity = restTemplate.postForEntity(url, formEntity, String.class);
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        //5、获取http状态码
        int statusCodeValue = stringResponseEntity.getStatusCodeValue();

        //6、获取返回体
        String body = stringResponseEntity.getBody();

        //7、映射实体类
        Test wrapper = JSONObject.parseObject(body, Test.class);


        return wrapper;

    }


    @RequestMapping("test3")
    public Object test3(){
        //1、构建body参数
        RestTemplate restTemplate =new RestTemplate();
        MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<String, String>();
        paramMap.set("id","dddd");
        paramMap.set("name","fff");

        //2、添加请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/x-www-form-urlencoded");

        //3、组装请求头和参数
        HttpEntity formEntity = new HttpEntity(paramMap, headers);

        //4、发起post请求
        ResponseEntity<String> stringResponseEntity = null;
        String url = "http://localhost:8090/test/test";
        try {
            stringResponseEntity = restTemplate.postForEntity(url, formEntity, String.class);
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        //5、获取http状态码
        int statusCodeValue = stringResponseEntity.getStatusCodeValue();

        //6、获取返回体
        String body = stringResponseEntity.getBody();

        //7、映射实体类
        Test wrapper = JSONObject.parseObject(body, Test.class);


        return wrapper;

    }

}

实体类

@Data
public class Test {
    private String id;

    private String name;
}
  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2022-06-01 15:24:57  更:2022-06-01 15:25:34 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 16:24:34-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码