客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

@RequestParam注解的参数

默认行为

参数前面什么都不加,默认是@RequestParam的作用但是,要保证请求的参数名和接收的参数名一致

如:
html页面中发送ajax请求附带参数名为 p

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>参数</title>
</head>
<body>

<!-- <a href="http://192.168.8.175:8087/test/param">a标签连接</a> -->
<p>========================</p>
<input type="submit" onclick="ajaxsubmit()" value = "ajax提交"/>

<script type="text/javascript">
	
	function ajaxsubmit () {
		var jsond = {
				param1 : 'aaa',
				param2 : 'bbb',
				param3 : 'ccc'
			}
		
		$.ajax({
			url:"http://192.168.8.175:8087/test/param?p=313",
			type:"post",
// 			data:JSON.stringify(jsond), // 将json对象转成json字符串
// 			data:jsond,
// 			dataType:"json",
// 			contentType : "application/json;charset=UTF-8",
			success:function(result) {	
				// doSomething
				console.log(result);
			}
		});
	}
	
</script>
</body>
</html>

参数接收方的 参数也必须包含 p

import java.io.BufferedReader;

import javax.servlet.http.HttpServletRequest;

import org.bouncycastle.util.encoders.BufferedDecoder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public String paramTest(String p) {
		System.out.println("p="+p);
		return p;
	}
	
}

测试:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
如果加了@RequestParam注解,参数名不必一样,但是注解@RequestParam的值必须与请求的参数名一致:

import java.io.BufferedReader;

import javax.servlet.http.HttpServletRequest;

import org.bouncycastle.util.encoders.BufferedDecoder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public String paramTest(@RequestParam("p") String pxxxxx) {
		System.out.println("p="+pxxxxx);
		return pxxxxx;
	}
	
}

@RequestParam注解的参数接收键值对格式的请求参数

键值对格式参数请求包括:

1. url地址后面拼接的参数:

$.ajax({
   		url:"http://192.168.8.175:8087/test/param?p=313",
   		type:"post",
// 			data:JSON.stringify(jsond), // 将json对象转成json字符串
// 			data:jsond,
// 			dataType:"json",
// 			contentType : "application/json;charset=UTF-8",
   		success:function(result) {	
   			// doSomething
   			console.log(result);
   		}
   	});

这里的 p=313就是一个键值对;

<a href="http://192.168.8.175:8087/test/param?p=111&p2=222">a标签连接</a>

这里的p=111&p2=222表示两个键值对,接收方分别需要两个参数 p 和 p2

2. Form表单提交的参数

请求方:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
接收方:

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public String paramTest(String param1,String param2,String param3) {
		return param1+param2+param3;
	}
	
}

3. 请求方参数为 json对象

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>参数</title>
</head>
<body>

<!-- <a href="http://192.168.8.175:8087/test/param?p=111&p2=222">a标签连接</a> -->
<p>========================</p>
<input type="submit" onclick="ajaxsubmit()" value = "ajax提交"/>

<script type="text/javascript">
	
	function ajaxsubmit () {
		var jsond = {
				param1 : 'a1',
				param2 : 'b2',
				param3 : 'c3'
			}
		
		$.ajax({
			url:"http://192.168.8.175:8087/test/param",
			type:"post",
// 			data:JSON.stringify(jsond), // 将json对象转成json字符串,传递的参数为json字符串
			data:jsond, // 传递的参数为 json 对象
			dataType:"text",
// 			contentType : "application/json;charset=UTF-8",
			success:function(result) {	
				// doSomething
				console.log(result);
			}
		});
	}
	
</script>
</body>
</html>

注意: 其中,data:jsond 表示的是json对象,**data:JSON.stringify(jsond)**表示的json字符串

  • json对象的本质是键值对, 所以接收方可以使用@RequestParam(默认不加)来接收。
  • json字符串的本质是字符串(废话!)。

接收方使用@RequestParam(默认不加)

package com.omomcam.controller.ca;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public String paramTest(String param1,String param2,String param3) {
		return param1+param2+param3;
	}
	
}

注意:因为接收方这里是直接返回的String字符串,所以发送方中ajax的dataType:“text”, 应该为text才能对应上,正常接收。

测试:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
一句话只要是键值对形式的请求参数,接收方都可以使用@RequestParam进行接收(默认不加)包括文件上传,可参考https://blog.csdn.net/qq_29025955/article/details/111318994 只要是键值对都行。

多个@RequestParam参数可封装成一个java对象来接收请求参数

接收方参数封装类:


public class TestParamEntity {
	
	private String param1;
	private String param2;
	private String param3;
	public String getParam1() {
		return param1;
	}
	public void setParam1(String param1) {
		this.param1 = param1;
	}
	public String getParam2() {
		return param2;
	}
	public void setParam2(String param2) {
		this.param2 = param2;
	}
	public String getParam3() {
		return param3;
	}
	public void setParam3(String param3) {
		this.param3 = param3;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((param1 == null) ? 0 : param1.hashCode());
		result = prime * result + ((param2 == null) ? 0 : param2.hashCode());
		result = prime * result + ((param3 == null) ? 0 : param3.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TestParamEntity other = (TestParamEntity) obj;
		if (param1 == null) {
			if (other.param1 != null)
				return false;
		} else if (!param1.equals(other.param1))
			return false;
		if (param2 == null) {
			if (other.param2 != null)
				return false;
		} else if (!param2.equals(other.param2))
			return false;
		if (param3 == null) {
			if (other.param3 != null)
				return false;
		} else if (!param3.equals(other.param3))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "TestParamEntity [param1=" + param1 + ", param2=" + param2 + ", param3=" + param3 + "]";
	}
}

接收方controller类


import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public TestParamEntity paramTest(TestParamEntity testParamEntity) {
		return testParamEntity;
	}
	
}

注意:controller中返回的是一个java对象(键值对),所以发送方 ajax中的datatype也要改成对应的键值对类型(即是json)

发送方:

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>参数</title>
</head>
<body>

<!-- <a href="http://192.168.8.175:8087/test/param?p=111&p2=222">a标签连接</a> -->
<p>========================</p>
<input type="submit" onclick="ajaxsubmit()" value = "ajax提交"/>

<script type="text/javascript">
	
	function ajaxsubmit () {
		var jsond = {
				param1 : 'a100',
				param2 : 'b200',
				param3 : 'c300'
			}
		
		$.ajax({
			url:"http://192.168.8.175:8087/test/param",
			type:"post",
// 			data:JSON.stringify(jsond), // 将json对象转成json字符串,传递的参数为json字符串
			data:jsond, // 传递的参数为 json 对象
			dataType:"json",
// 			contentType : "application/json;charset=UTF-8",
			success:function(result) {	
				// doSomething
				console.log(result);
			}
		});
	}
	
</script>
</body>
</html>

测试:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

@RequestBody注解的参数

@RequestBody的解释及原理有篇文章写得很不错:https://blog.csdn.net/justry_deng/article/details/80972817/?ivk_sa=1024320u

哪些场景可以使用@RequestBody

适用场景: 直接发送一坨数据,不是键值对格式。

比如:现在要将 字符串 “lallalalalaa啦啦啦啦啦啦啦” 发送给接收方,没有任何参数(无论是请求方还是接受方都没有指定参数)

场景1:使用ajax发送请求

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>参数</title>
</head>
<body>

<!-- <a href="http://192.168.8.175:8087/test/param?p=111&p2=222">a标签连接</a> -->
<p>========================</p>
<input type="submit" onclick="ajaxsubmit()" value = "ajax提交"/>

<script type="text/javascript">
	
	function ajaxsubmit () {
		var jsond = {
				param1 : 'a100',
				param2 : 'b200',
				param3 : 'c300'
			}

		$.ajax({
			url:"http://192.168.8.175:8087/test/param",
			type:"post",
// 			data:JSON.stringify(jsond), // 将json对象转成json字符串,传递的参数为json字符串
			data:"lallalalalaa啦啦啦啦啦啦啦", // 就是一坨字符串
// 			data:jsond, // 传递的参数为 json 对象
			dataType:"text",
// 			contentType : "application/json;charset=UTF-8",
			contentType : "application/text;charset=UTF-8",
			success:function(result) {	
				// doSomething
				console.log(result);
			}
		});
	}
	
</script>
</body>
</html>


注意
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

接收方:


import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public String paramTest(@RequestBody String p) {
		return p;
	}
	
}

注意
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
接上图说明:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
测试:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

- 场景1.1 (属于场景1的一种)

将那一坨数据 “lallalalalaa啦啦啦啦啦啦啦” 改成json格式的字符串,注意,一定要是字符串,如果是json对象(即是键值对)就是属于 “@RequestParam注解的参数”那种情况了。

发送方:

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>参数</title>
</head>
<body>

<!-- <a href="http://192.168.8.175:8087/test/param?p=111&p2=222">a标签连接</a> -->
<p>========================</p>
<input type="submit" onclick="ajaxsubmit()" value = "ajax提交"/>

<script type="text/javascript">
	
	function ajaxsubmit () {
		var jsond = {
				param1 : 'a100a',
				param2 : 'b200b',
				param3 : 'c300c'
			}

		$.ajax({
			url:"http://192.168.8.175:8087/test/param",
			type:"post",
			data:JSON.stringify(jsond), // 将json对象转成json字符串,传递的参数为json字符串
// 			data:"lallalalalaa啦啦啦啦啦啦啦", // 就是一坨字符串
// 			data:jsond, // 传递的参数为 json 对象
			dataType:"text",
			contentType : "application/json;charset=UTF-8",
// 			contentType : "application/text;charset=UTF-8",
			success:function(result) {	
				// doSomething
				console.log(result);
			}
		});
	}
	
</script>
</body>
</html>


注意:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
接收方不变:

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public String paramTest(@RequestBody String p) {
		return p;
	}
	
}

测试:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

- 场景1.2 :发送方发送一坨json格式的字符串,接收方使用java对象来接收并封装数据。

发送方:

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<title>参数</title>
</head>
<body>

<!-- <a href="http://192.168.8.175:8087/test/param?p=111&p2=222">a标签连接</a> -->
<p>========================</p>
<input type="submit" onclick="ajaxsubmit()" value = "ajax提交"/>

<script type="text/javascript">
   
   function ajaxsubmit () {
   	var jsond = {
   			param1 : 'a100aaa',
   			param2 : 'b200bbb',
   			param3 : 'c300ccc'
   		}

   	$.ajax({
   		url:"http://192.168.8.175:8087/test/param",
   		type:"post",
   		data:JSON.stringify(jsond), // 将json对象转成json字符串,传递的参数为json字符串
// 			data:"lallalalalaa啦啦啦啦啦啦啦", // 就是一坨字符串
// 			data:jsond, // 传递的参数为 json 对象
   		dataType:"text",
   		contentType : "application/json;charset=UTF-8",
// 			contentType : "application/text;charset=UTF-8",
   		success:function(result) {	
   			// doSomething
   			console.log(result);
   		}
   	});
   }
   
</script>
</body>
</html>

接收方:

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public TestParamEntity paramTest(@RequestBody TestParamEntity testParamEntity) {
		return testParamEntity;
	}
	
}

注意
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
测试:

客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
发送方的 dataType改成 json:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
测试:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

场景2: 使用httpClient发送请求

新建一接口用于触发httpclient请求

java

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import com.alibaba.fastjson.JSON;

@RestController
public class HttpClientController {
   
   
   @Autowired
   private RestTemplate template;

   @PostMapping("/http/client/test")
   public String httpClientTest() {
   	//设置请求头
   	 HttpHeaders httpHeaders = new HttpHeaders();
   	 httpHeaders.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE); // 设置contentType的类型为  application/json
   	//设置请求体
   	 Map<String, Object> params = new HashMap<>();
   	 params.put("param1", 1);
   	 params.put("param2", 2);
   	 params.put("param3", 3);
   	 
   	 String jsonData = JSON.toJSONString(params);  // 一坨请求参数 !!(字符串类型!!)
   	 
   	 System.out.println("..................请求的参数jsonData="+jsonData);
   	
   	 HttpEntity<String> request = new HttpEntity<>(jsonData, httpHeaders); // 将请求头(contentType)和请求体(一坨数据)
   	 
   	 String httpUrl = "http://localhost:8087/test/param"; // 请求的url
   	
   	 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(httpUrl);
   	 
   	 TestParamEntity testParamEntity = template.postForObject(builder.toUriString(), request, TestParamEntity.class); // 发送请求
   	 
   	 System.out.println("..................返回的数据testParamEntity="+testParamEntity);
   	 
   	 return "测试成功!!";
   	 
   }
   
   
}

客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

原理与ajax发送请求一致,只是使用的是java代码来实现

客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
测试:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
执行结果:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

场景3:使用postman快速测试

客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
接收方:

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public TestParamEntity paramTest(@RequestBody TestParamEntity testParamEntity) {
		return testParamEntity;
	}
	
}

格式选择Text ,不选择JSON的情况

客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

选择了Text ,则在Headers中的content-type就为text/plain
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
此时,接收方还是使用java对象来接收就会报错:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
测试:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

解决办法:
1、要么把接收方的参数类型TestParamEntity 改为 Stirng
或者
2、把headers中content-type改为application/json

场景4:其他形式发送请求,只要符合规则即可

@RequestParam注解与@RequestBody注解混合使用

注意:@RequestBody在一个接口最多只能有一个,而@ReqeustParam可以有多个

使用postman测试

接收方:


import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RequestParamController {
	
	@PostMapping("/test/param")
	@CrossOrigin
	public String paramTest(@RequestBody TestParamEntity testParamEntity, String param4,String param5) {
		return testParamEntity + param4 + param5;
	}
	
}

发送方:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结
测试:
客户端与springboot REST接口参数@RequestParam @RequestBody交互总结

网上写的不错的文章:https://blog.csdn.net/f123147/article/details/114832871

上一篇:AtCoder Regular Contest 133


下一篇:@RequestParam、@RequestBody、@PathVariable区别和案例分析