Behind every successful person, who must have made brave and lonely decision.
Giving up is not difficult, but insisted cool ~
I. Introduction
There has been such a question: when using the postman api interface test tools, how to use the json
string by value of it, instead of using x-www-form-urlencoded
type, after all, by key-value
traditional values have limitations. If I want to test bulk insert data interface to it, using x-www-form-urlencoded
the method simply does not apply in this scenario.
So how do you use json string by value by postman tool does, here arise out of the spring's two notes:
- @RequestParam
- @RequestBody
All in all, these two parameters can receive notes in the background, but not the same usage scenario. Continue to look down ↓
二、@RequestParam
First introduce @RequestParam usage scenarios:
Annotation @RequestParam parameter is received from requestHeader , i.e. the request header . GET requests typically used , such as common URL: HTTP: // localhost: 8081 / Boot-Spring-Study / Novel / findByAuthorAndType author = & type = Tang three little has ended? , In which Controller
the following layers shown in FIG written:
@RequestParam There are three configuration parameters:
required
Must indicate whether, by defaulttrue
, you must.defaultValue
Default parameters may be provided in the request.value
Url name to receive a parameter (corresponding to key value).
@RequestParam for processing Content-Type
of application/x-www-form-urlencoded
the encoded content, Content-Type
the default for this property.
@RequestParam may also be used for other types of requests, such as: POST, DELETE and so on request . Such as inserting data into a single table, Controller
writing layer is shown below:
Since @RequestParam process is used Content-Type
to application/x-www-form-urlencoded
encode content, so the postman, the body type is to be selected x-www-form-urlencoded
, so that the headers are automatically becomes Content-Type
: application/x-www-form-urlencoded
encoding format. As shown below:
But this does not support bulk insert data ah, if you use json
the string to pass value, the type is set application/json
, click Send, then, will complain, background not receive value for null
.
At this time, notes @RequestBody comes in handy. Continue to look down ↓
三、@RequestBody
First introduce @RequestBody usage scenarios:
Annotation @RequestBody parameter is received from requestBody , i.e. the request body . Usually for processing the non- Content-Type: application/x-www-form-urlencoded
data encoding format, such as: application/json
, application/xml
and other types of data.
On the application/json
type of data, you can use annotations @RequestBody body inside all json data to the back-end, back-end and then parse.
3.1 Bulk insert data into the table
For example, a batch of data is inserted, the wording Controller layer is shown below:
由于@RequestBody可用来处理 Content-Type
为 application/json
编码的内容,所以在postman中,选择body的类型为row
-> JSON(application/json)
,这样在 Headers
中也会自动变为 Content-Type
: application/json
编码格式。body内的数据如下图所示:
批量向表中插入两条数据,这里的 saveBatchNovel()
方法已经封装了 JPA
的 saveAll()
方法。body
里面的 json
语句的 key
值要与后端实体类的属性一一对应。
注意:前端使用$.ajax的话,一定要指定 contentType: "application/json;charset=utf-8;"
,默认为 application/x-www-form-urlencoded
。
3.2 后端解析json数据
上述示例是传递到实体类中的具体写法,那么如果传递到非实体类中,body里面的json数据需要怎么解析呢?我们再来看下面这个例子:
在body中,我们还是输入上面的json数据,根据分析,上面的json数据是一个List数组内嵌套着map对象,那么在后台的接收形式可写为 List<Map<String, String>>
,具体代码如下图所示:
postman请求:
控制台输出:
得出结论,通过@RequestBody可以解析Body中json格式的数据。
四、总结
注解@RequestParam接收的参数是来自requestHeader中,即请求头。通常用于GET请求,像POST、DELETE等其它类型的请求也可以使用。
注解@RequestBody接收的参数是来自requestBody中,即请求体。一般用于处理非 Content-Type: application/x-www-form-urlencoded
编码格式的数据,比如:application/json
、application/xml
等类型的数据。通常用于接收POST、DELETE等类型的请求数据,GET类型也可以适用。
总算把这两个的逻辑理清楚了,postman也会用json传值了!赶紧整理成笔记,与大家分享?
源码已上传至https://github.com/841809077/spring-boot-study,欢迎Star。
求大家点点好看??,给点鼓励。