Postman transmission request, and receiving background

I. Introduction

When using the postman api interface test tools, how to use the jsonstring by value of it, instead of using x-www-form-urlencodedtype, after all, by key-valuetraditional values have limitations. If I want to test bulk insert data interface to it, using x-www-form-urlencodedthe 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 Controllerthe following layers shown in FIG written:

 

 

@RequestParam There are three configuration parameters:

  • requiredMust indicate whether, by default true, 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-Typeto application/x-www-form-urlencodedencode 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-urlencodedencoding format. As shown below:

But this does not support bulk insert data ah, if you use jsonthe 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-urlencodeddata encoding format, such as: application/json, application/xmland other types of data.

On the application/jsontype 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-Typeis application/jsonencoded contents, so the postman, the body type is selected row-> JSON(application/json), so that Headersautomatically becomes in Content-Type: application/jsonencoding format. Data in the body as shown below:

批量向表中插入两条数据,这里的 saveBatchNovel()方法已经封装了 JPAsaveAll() 方法。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/jsonapplication/xml等类型的数据。通常用于接收POST、DELETE等类型的请求数据,GET类型也可以适用。

@requestBody一般是将多个参数封装为一个对象时使用。访问方式为application/json 一般为post

@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");

 

 

 

Guess you like

Origin www.cnblogs.com/lvhouhou/p/11973762.html