Springboot get / post request details

Programmer light 2022-02-13 07:58:44 阅读数:538

springboot post request details

Catalog

One 、 Common parameter form

Basic data type ( With int For example )

  Packing type parameter binding

Custom object type parameter binding

Custom composite object type parameter binding

List Parameter binding

Map Parameter binding

@RequestParam Annotation analysis

1) Parameter limits ( You can use required=false close )

2) Set parameter defaults

3) Custom parameter name

Array type parameter binding

1) Basic array type receive parameters

2)List Type receives parameters

Two 、POST request :Json Mode parameter binding

1、@RequestBody Annotation analysis

2、json The mode is directly bound to the user-defined object type

3、json Custom object direct binding mode

4、json Pattern binding array type ( Generally used for batch operation )

5、json Pattern binding multiple objects ( Same parameter type )

1) Use List obtain

2) Use Map receive


This introduction is based on SpringBoot Next get/post request

One 、 Common parameter form

Basic data type ( With int For example )

stay controller Write a int Parameter binding method

 @GetMapping("/getInt")
public String getInt(int id){
return "ID = " + id;
}

summary :

1) When binding parameters with basic types , You have to pass in key value , And value Value must be a declared base type , If the data submitted on the page is null or “” Data conversion exceptions will occur , Therefore, it is best to use the packing type parameter

2) Front end parameter name and controller Data binding can be completed when the parameter names of are consistent , Inconsistencies can be used @RequestParam

 @GetMapping("/getInt2")
public String getInt2(@RequestParam(value = "ik") int id){
return "IK = " + id;
}

  Packing type parameter binding

stay controller Write the method of binding multiple wrapper type parameters in

 @GetMapping(value = "/getUser",produces = {"application/json;charset=UTF-8"})
public String getUser(String name,Integer age){
return "name = " + name + " age = " + age;
}

summary :

1) When binding parameters with wrapper type, it is the same as the basic data type , Transmitted key The value should be consistent with the parameter name bound inside

2) When binding a parameter to a wrapper type, the value of the parameter may not be passed to null, It can also be empty

Custom object type parameter binding

 @GetMapping("/getImtes")
public Items getImtes(Items items){
return items;
}

  summary :1) Just match the property name of the object with the property name of the front end input The parameter names of the are consistent

Custom composite object type parameter binding

 @GetMapping(value = "/getOrderForm",produces = {"application/json;charset=utf-8"})
public OrderForm getOrderForm(OrderForm orderForm){
return orderForm;
}

summary :

1) Custom composite object types are used in the same way as custom object types

2) Custom composite object types input The parameter name of should use “ Property name ( Properties of the object type ). Property name " Named after the

List Parameter binding

@GetMapping(value = "/getIds",produces = {"application/json;charset=utf-8"})
public List<Long> getIds(@RequestParam("ids") List<Long> ids){
return ids;
}

summary :

1) Front end transfers are separated by commas

Map Parameter binding

 @GetMapping(value = "/getMap")
public Map<String,Object> getMap(@RequestParam Map<String,Object> map){
return map;
}

 

summary :

1) need RequestParam annotation

@RequestParam Annotation analysis

@RequestParam The function of annotation is limited by parameters 、 Set parameter defaults 、 Custom parameter name

1) Parameter limits ( You can use required=false close )

 @GetMapping(value = "/getName",produces = {"application/json;charset=utf-8"})
public String getName(@RequestParam(required = false) String name){
return name;
}

2) Set parameter defaults

 @GetMapping("/defaultValue")
public Integer defaultValue(@RequestParam(defaultValue = "100") Integer num){
return num;
}

3) Custom parameter name

You can try it yourself

summary :

1)@RequestParam Annotation settings change parameter names 、 Set parameter limits and set parameter defaults , It can be used according to different scenarios

2)@RequestParam You can use multiple

Array type parameter binding

1) Basic array type receive parameters

 @GetMapping("/getArray")
public String[] getArray(String[] arr){
return arr;
}

  summary : It's simple , Just use the array type to receive

2)List Type receives parameters

 @GetMapping("/getList")
public List<String> getList(@RequestParam List<String> names){
return names;
}

  summary :1) You can't be short of @RequestParam annotation , Otherwise, an error will be reported

Two 、POST request :Json Mode parameter binding

Due to the separation of front and rear ends and the diversity of front ends , Usually we use json Data format parameters / Data transfer , Speaking of json Format , You have to say @RequestBody, This is the use of Json Pattern is an essential part of parameter binding

1、@RequestBody Annotation analysis

1)@RequestBody The role of annotations will be json Format data to java object

2)@RequestBody It is often used to deal with application/json type

3)@RequestBody It received a json Format string

2、json The mode is directly bound to the user-defined object type

@PostMapping("/postItems")
public Items postItems(@RequestBody Items items){
return items;
}

summary :

1) direct json The string is converted to javaBean, But the front-end transmission must be json Formatted data

2) The parameter name must always be the same as the attribute name defined by the entity class to get the parameter correctly

3、json Custom object direct binding mode

 @PostMapping(value = "/postOrderForm",produces = {"application/json;charset=utf-8"})
public OrderForm postOrderForm(@RequestBody OrderForm orderForm){
return orderForm;
}

summary :

1) Both require the parameter name to be consistent with the attribute name defined by the entity class

2) Direct access , The parameter name of the hierarchy needs to be . Appoint ;@RequestBody Formal parameters json The format needs to be nested

4、json Pattern binding array type ( Generally used for batch operation )

controller Use @RequestBody+List Receive array ( recommend )

 @PostMapping("/postIds")
public List<Integer> postIds(@RequestBody List<Integer> ids){
return ids;
}

summary :

1)@RequestBody obtain List Array type parameters are widely used

2) Later, you can list The content in the is transformed into String type , With "," Division

5、json Pattern binding multiple objects ( Same parameter type )

1) Use List<E> obtain

 @PostMapping("/postItemList")
public List<Items> postItemList(@RequestBody List<Items> items){
return items;
}

2) Use Map<String,Object> receive

 @PostMapping("/postMapItems")
public Map<String,Items> postMapItems(@RequestBody Map<String,Items> map){
return map;
}

copyright:author[Programmer light],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130758422672.html