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 @ RequestBody in java

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to use @ RequestBody in java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Introduction to the basics:

@ RequestBody is mainly used to receive the data (data in the request body) in the json string passed by the frontend to the backend; GET method has no request body, so when using @ RequestBody to receive data, the frontend cannot submit data in GET mode, but in POST mode. In the same receiving method at the back end, @ RequestBody and @ RequestParam () can be used at the same time, @ RequestBody can have at most one, and @ RequestParam () can have multiple.

Note: one request, only one RequestBody;, one request, can have multiple RequestParam.

Note: when @ RequestParam () and @ RequestBody are used at the same time, the parameters specified by @ RequestParam () can be normal elements,

Arrays, collections, objects, etc. (that is, when @ RequestBody and @ RequestParam () can be used together, the original SpringMVC receives

The mechanism of parameters remains the same, except that RequestBody receives the data in the request body, while RequestParam receives key-value

The parameters inside, so it will be processed by section so that it can be received with ordinary elements, arrays, collections, objects, etc.).

That is, if the parameter is placed in the request body and passed into the background, the backend needs to use @ RequestBody to receive it; if it is not placed in the

In the request body, then when the background receives the parameters sent by the foreground, it should use @ RequestParam to receive, or the formal parameter before.

You can accept it without writing anything.

Note: if @ RequestParam (xxx) is written before the parameter, then the front end must have a corresponding xxx name (whether it has a value or not, of course.

Set the required property of the annotation to adjust whether it must be passed.), if there is no xxx name, then the request will make an error.

Note: if @ RequestParam (xxx) is not written before the parameter, then the front end may not have a corresponding xxx name, if there is a xxx name

If so, it will match automatically; without it, the request will be sent correctly.

Note: this is different from when feign consumes services; when feign consumes services, if nothing is written before the parameters, it will be defaulted to

@ RequestBody.

If the backend parameter is an object and is preceded by @ RequestBody, the following requirements must be met when the json parameter is passed by the frontend:

When the class corresponding to the backend @ RequestBody annotation assembles the input stream of HTTP (including the request body) to the target class (that is, the class following @ RequestBody), it will match the attributes of the corresponding entity class according to the key in the json string. If the matching is consistent and the corresponding value of the key in json matches (or can be converted to), I will analyze this item in detail below, and the rest can be simply skipped. But the core logic code at the end of this article and several conclusions must be read! When the type of the corresponding attribute of the entity class is required, the setter method of the entity class is called to assign the value to the property.

In the json string, if value is "", if the backend corresponding attribute is of type String, then you will receive "". If the type of backend attribute is Integer, Double, etc., then you will receive null.

In the json string, if value is null, the null is received at the backend.

If a parameter does not have value, when passing the json string to the backend, either the field is not written into the json string at all, or when writing value, it must have a value, either null or "". Never write something like "stature":, such as:

The example details:

First give two entity classes to be used later.

User entity class:

Team entity class:

Use PostMan to test:

Use PostMan to test:

Use PostMan to test:

Use PostMan to test:

@ RequestBody is used with the complex @ RequestParam ():

Controller corresponding to the backend:

@ RequestBody receives the json data in the request body; receives the data in the URL without annotation and assembles it into an object:

Controller corresponding to the backend:

Use PostMan to test:

Tips: what key do the attributes in the model correspond to?

Here is a brief introduction, more reference:

Public class BeanPropertyMap implements Iterable,java.io.Serializable

Give the test classes in Controller:

Give the attributes in the model (the setter/getter method is not truncated):

Test it with postman, for example:

This is how @ RequestBody is used in the java shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report