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.
二、@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 =? Tang three little & type = 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 Content-Type is application / x-www-form-urlencoded encoding, Content-Type default for this property.
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 ↓
Three, @ 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:
Since the process can be used to @RequestBody Content-Type
is application/json
encoded contents, so the postman, the body type is selected row
-> JSON(application/json)
, so that Headers
automatically becomes in Content-Type
: application/json
encoding format. Data in the body as shown below:
批量向表中插入两条数据,这里的 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类型也可以适用。
@requestParam实例: 访问方式可为form表单 一般为get
public AjaxResult getCouponCode( @RequestParam(name = "userId", required = false, defaultValue = "-1") long userId)
当接口请求是application/JSON时,直接发Json.toJSONString(对象)
public RebateRuleDTO queryRebateRule(GetRebateRule rebateRule) { return getObject(URLRebateConstant.URL_QUERY_REBATE_RULE, JSON.toJSONString(rebateRule), RebateRuleDTO.class, "queryRebateRule");