一、@RequestParam注解
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
(1)@RequestParam 注解三个参数说明:
a)value、name 属性都标识请求参数名(必须配置);
b)required:参数是否必传,默认为 true,可以设置为非必传 false;(如果设置了必传或默认,请求未传递参数,将会抛出异常);
c)defaultValue:参数默认值,如果设置了该值,required 将会自动设置为 false;
(2)@RequestParam 注解使用场景:
@RequestParam注解接收的参数大多数场景是来自是url中,格式为:http://localhost:8093?name=admin&password=123456
@RequestParam用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性。
@RequestParam也可用于其它类型的请求,例如:POST、DELETE等请求。比如向表中插入单条数据,Controller 层的写法如下图所示:
以上大多是其它文章看到的,确实如此,但是实际场景使用不止这些,如下先写个接口再测试模拟三种以上请求场景查看对应结果:
接口大致如图:
场景一成功(地址参数方式请求):
对应curl
curl --location --request POST 'http://localhost:8093/login?name=tea1&password=123456' \
--header 'Cookie: second_token=004af4235b5d4bb1b33ae00409712734'
场景二成功(请求体中form-data方式请求):
对应curl
curl --location --request POST 'http://localhost:8093/login' \
--header 'Cookie: second_token=3c2cb9da62744b548f01337eecd08875' \
--form 'password="123456"' \
--form 'name="tea1"'
场景三成功(请求体中x-www-form-urlencoded方式请求):
对应curl
curl --location --request POST 'http://localhost:8093/login' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: second_token=61fd704bbf7749129d2b7c162bc06beb' \
--data-urlencode 'name=tea1' \
--data-urlencode 'password=123456'
场景四失败(请求体中raw的json方式请求):
对应curl
curl --location --request POST 'http://localhost:8093/login' \
--header 'Content-Type: application/json' \
--header 'Cookie: second_token=61fd704bbf7749129d2b7c162bc06beb' \
--data-raw '{
"name":"tea1",
"password":"123456"
}'
服务端错误信息
二、@RequestBody注解
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {
/**
* Whether body content is required.
* <p>Default is {@code true}, leading to an exception thrown in case
* there is no body content. Switch this to {@code false} if you prefer
* {@code null} to be passed when the body content is {@code null}.
* @since 3.2
*/
boolean required() default true;
}
(1)@RequestBody注解只拥有一个参数:
a)required 默认为 true,即对象中的属性必须有一个要传,否则会抛出异常:
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing
(2)@RequestBody注解获取的参数在请求哪?
a)post请求的requestHeaders请求头中有content-type字段,一般用来处理:applicatin/json格式的参数;
b)Spring中的@RequestBody注解是用来接收请求体中的参数数据,即requestBody请求体中,故`不受参数数据长度的限制`;
接着如下修改服务端接口再测试模拟json请求方式查看对应结果:
服务端接口
测试成功结果
对应curl
curl --location --request POST 'http://localhost:8093/login' \
--header 'Content-Type: application/json' \
--header 'Cookie: second_token=b62062f815eb4e3b9cd69e83105c26f6' \
--data-raw '{
"name":"tea1",
"password":"123456"
}'
额外补充content-type属性相关值 :
1、application/x-www-form-urlencoded 这种就是一般的文本表单用 post 传地数据,
2、multipart/form-data ,用于文件上传,此时 form 的 enctype 属性必须指定为 multipart/form-data;
3、application/json,数据以json的格式传递;
4、application/xml,数据以 xml 格式传输
6、image/png; 图片上传 图片格式png
参考文章:
https://www.cnblogs.com/blogtech/p/11172168.html
https://cloud.tencent.com/developer/article/1414464