RequestBody and RequestParam
Two annotations that students often use when passing JSON data back and forth.
@requestBody and @RequestParam are primarily used to receive JSON data from the front end to the back end. In general, the @requestbody annotation is used in post requests because the front end places the json data in the RequestBody. @requestBody can be used with @requestParam in a method received at the back end, but there can only be one @RequestBody and multiple @RequestParam in a method. RequestParam can be used in GET requests to receive normal elements, arrays, collections, objects, etc.
If the parameter is in the RequestBody, the incoming background needs to receive it with @requestbody. RequestParam (@requestParam, @requestParam, @requestParam, @requestParam);
To use @requestParam (XXX), the front-end must have the corresponding XXX name, which can be adjusted by setting the reqired attribute.
If @requestParam (XXX) is not written, the method argument will be automatically matched and will default to null if the name is different.
If the back-end receive parameter is an object, and the parameter is decorated with @requestBody, then the front-end JSON passes the data with:
- When the back end assembs the HTTP input stream to the target class, it matches the attributes of the corresponding entity class according to the key in the JSON string. If the match is consistent and the key in the JSON matches, the back end can receive it successfully
- If the value of a JSON String is “”, the value of a String attribute is “”; if the value of a String attribute is a reference type Integer, Double, etc., the value is received as “”
- In a JSON string, if the value is null, the backend receives null
- If a parameter does not have a value, when passing JSON to the back end, either the lid side should not be written to json at all, or the value should be assigned a value of “” or null
RequestBody uses method one
There’s aPerson
Class, and the corresponding properties areString name,Integer age
, can be accessed throughString
To receive the JSON string
RequestBody uses method two
You can use objects to receive front-end JSON strings
Request Method 3
You can use a mixture of the former and the latter. If you have a complex object (a class that combines other classes), you can use the object to receive it, or you can override the toString() method of the combined object to return a JSON string to receive it as a string.
When the backend accepts the argument as an object, if the class attribute definition value does not correspond to the JSON key, you can use JsonAlias or JsonProperty to specify the change.
ResponseBody
The @responseBody annotation is used to return JSON data to the front end. If you’re lazy and just want to return an object, the @responseBody annotation will automatically convert the returned object to JSON and return it to the front end.
You can use @restController instead of @responseBody in SpringBoot. If you do not use @responseBody, you will get various errors (NumberFormatException… For the input String)