@PathVariable, @RequestParam and @RequestBody annotation introduction and specific use in conjunction with PostMan request [detailed tutorial]

        This weekend, I suddenly remembered that a friend asked me a few days ago, how does postman request an interface with @PathVariable annotations, I thought about it carefully, why not give him a tutorial, and introduce how the three annotations correspond to postman's request to pass parameters ,

Let me give an example, try to consider as many different types of parameters as possible for parameter passing demonstration!

table of Contents:

1、@RequestParam 

1. Params way

2. Form-data method

3、application/x-www-form-urlencoded方式

2、@RequestBody 

1. Use entity class to connect to parameters:

 2. Use map to connect to parameters:

3 、 @ PathVariable

4. Combined use:

@PathVariable and @Requestbody combination


 Let's first understand, what are the access parameter annotations provided by SpringBoot?

First of all, let me introduce:

1、@RequestParam 

As the name implies: Get parameters, that is, get the transmitted parameters;

Example request: http://xxxxx?id=xxxxxx

Example: Use @requestParam to obtain the following different types of parameters

@PostMapping("test2")
public R TestTwo(@RequestParam(value = "page", defaultValue = "1") String page,
                 @RequestParam(value = "size") Integer size,
                 @RequestParam(value = "bool",required = false) Boolean bool,
                 @RequestParam(value = "id") Long id
) {
    HashMap<String, Object> map = new HashMap<>(16);
    //直接获取参数
    map.put("page",page);
    map.put("size",size);
    map.put("bool",bool);
    map.put("id",id);
    return R.ok().data(map);
}

The specific postman request url: localhost:8081/test/test2?page=2&size=5&bool=true&id=202010182020101820201018

1. Params way

: Use the Params method in Postman to pass parameters, or pass parameters in Parms

2. Form-data method

: Use the form-data method in Postman

 

3、application/x-www-form-urlencoded方式

: Use the application/x-www-form-urlencoded method in Postman

 

Note: The @RequestParam annotation is used above. If the type of the parameter is Long, if the parameter is too long, a conversion exception will be reported

Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "202010182020101820201018"

2、@RequestBody 

@RequestBodyAnnotations are generally mainly used to process content-type:"application/json charset=utf-8"or content-type:"application/xml charset=utf-8"two kinds of request data, usually asynchronous requests are more used; when the front end or others pass in JSON data to you, use this annotation, and it will be passed in to your field or attribute Bind it with the entity class you created; or use map to receive it.

Example request: http://xxxxx/xxx

1. Use entity class to connect to parameters:

@PostMapping("test5")
public R TestFive(@RequestBody TestEntity testEntity) {
    HashMap<String, Object> mapNew = new HashMap<>(16);
    mapNew.put("page", testEntity.getPage());
    mapNew.put("size", testEntity.getSize());
    mapNew.put("bool", testEntity.getBool());
    mapNew.put("id", testEntity.getId());
    return R.ok().data(mapNew);
}

entity:

@Data
public class TestEntity { 
    private String page;
    private Integer size;
    private Boolean bool;
    private Long id;
}

2. Use map to connect to parameters:

When the parameters passed by the front end or others do not match your own entity, or when the parameters passed by others do not match the attributes of your current entity class, then you need Map to receive it, because JSON Isn't the data K, V? So we use Map to install it, the situation is as follows:

@PostMapping("test3")
public R TestThree(@RequestBody Map<String, Object> map) {
    HashMap<String, Object> mapNew = new HashMap<>(16);
    mapNew.put("page", map.get("page"));
    mapNew.put("size", map.get("size"));
    mapNew.put("bool", map.get("bool"));
    mapNew.put("id", map.get("id"));
    return R.ok().data(mapNew);
}

The results of get post methods are the same, so I won’t give examples one by one!

3 、 @ PathVariable

As the name implies: path variable, which is to get the variable on the link path

Example request: http://xxxxx/{xxx}/{xxx}

It is generally used for get requests (the same is true for post requests), namely XXX/{XXXid}. At this time, the XXXid can be bound to the method parameters through the @Pathvariable annotation, such as:

@GetMapping("test6/{page}/{size}/{bool}/{id}")
public R TestSix(@PathVariable("page") String page,
                 @PathVariable("size") Integer size,
                 @PathVariable("bool") Boolean bool,
                 @PathVariable("id") Long id) {
    HashMap<String, Object> map = new HashMap<>(16);
    map.put("page", page);
    map.put("size", size);
    map.put("bool", bool);
   map.put("id", id);
    return R.ok().data(map);
}

Note: Pay attention to the corresponding position of each parameter when passing parameters in the path

4. Combined use:

@PathVariable and @Requestbody combination

1. For example, the combination of @PathVariable and @Requestbody actually has many annotation combinations, so I won’t give examples one by one.

@PostMapping("test8/{id}")
public R TestEight(@PathVariable("id") Long id,
                   @RequestBody TestEntity testEntity) {
    HashMap<String, Object> mapNew = new HashMap<>(16);
    mapNew.put("page", testEntity.getPage());
    mapNew.put("size", testEntity.getSize());
    mapNew.put("bool", testEntity.getBool());
    mapNew.put("id", id);
    return R.ok().data(mapNew);
}

To be honest, it's too fancy, who would use it in such a combination usually, the most commonly used ones are @Requestbody and @RequestParam, okay, that's all for today's introduction, see you next time!


  Reference:

       https://blog.csdn.net/W_317/article/details/107108538

       https://www.cnblogs.com/chengxiaodi/p/11324611.html


❤If the article is helpful to you, please click like at the top right corner of the article or at the end of the article! (づ ̄ 3 ̄)づ 

❤If you like the articles shared by the white rabbit, please pay attention to the white rabbit! (๑′ᴗ‵๑)づ╭❤~

❤If you have any questions about the article, please leave a message below or join the group to discuss [group number: 708072830]

❤In view of the limited personal experience, all opinions and technical research points, if you have any objections, please reply directly to the discussion (do not make offensive remarks)

Guess you like

Origin blog.csdn.net/weixin_43970743/article/details/109143192