Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ RequestParam and @ RequestBody

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use @ RequestParam and @ RequestBody. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The front end passes parameters to the back end. How the back end receives it, you will think of the comments of spring.

Before that, I always used the form of RequestParam ("userName") String userName, but I didn't use RequestParam String userName very much when I was reasonable.

Unwittingly, it's time to see what's the difference between the two.

@ RequestParam @ RequestBody (received as json data)

If you do not write the @ RequestParam (xxx) annotation, then the front end can have or have no corresponding xxx name.

If there is a xxx name, it will match automatically.

If not, the request can be sent correctly.

1. @ RequestParam

/ * * user logs in * @ param userName * @ param password * @ return * / @ RequestMapping (value = "login", method = RequestMethod.POST) public String login (@ RequestParam String userName, @ RequestParam String password) {public BaseResponse login (@ RequestParam ("user") String userName, @ RequestParam ("psd") String password) {

If the userName or password parameter name is not passed in the front end, an error will be reported.

1. The parameter name passed from the @ RequestParam front end defaults to the parameter name after our String in userName.

If the user or psd parameter name is not passed in the front end, an error will be reported.

two。 Specify the parameter name for the parameter passed by the front end with @ RequestParam ("user") @ RequestParam ("psd")

Error message

3. "message": "Required String parameter 'userName' is not present"

Actually, there are other forms.

Look down there.

@ RequestParam (value= "user" required=false)

You can use required = false or true to ask whether the frontend parameters of @ RequestParam configuration must be passed.

Required = false means that if it is not passed, the parameter will be assigned a value of null. If required = true, it is required.

Note:

If the parameter of the @ requestParam annotation is of type int and required=false, an error will be reported if no parameter is passed at this time. The reason is that when required=false, if no parameter is passed, the parameter will be assigned a value of null, so the null will be assigned to int, so an error will be reported.

If you use the Integer wrapper type, you should also pay attention to null pointer exceptions

2. @ RequestBody

@ RequestBody receives the json data in the request body sent by the front end as a string

@ RequestMapping (value = "login", method = RequestMethod.POST) public String login (@ RequestBody String jsonStr) {

@ RequestBody receives json data from the front end as a simple object

@ RequestMapping (value = "login", method = RequestMethod.POST) public String login (@ RequestBody User user) {

@ RequestBody receives json data from the front end as a complex object

@ RequestMapping (value = "login", method = RequestMethod.POST) public String login (@ RequestBody Team team) {

Thank you for reading! This is the end of the article on "how to use @ RequestParam and @ RequestBody". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report