The request body is the data a client sends inside a POST or PUT request, usually as JSON. @RequestBody tells Spring to read that JSON and convert it into a Java object automatically, using the Jackson library (this is called deserialization). It is used for sending structured data such as registration forms, orders, or profile updates, which is too large or too private for the URL. JSON keys are matched to the Java field names, nested JSON objects become nested Java objects, and JSON arrays become lists.
Types
- Object binding:
@RequestBody RegisterRequest request(JSON to a DTO class). - Validated body:
@Valid @RequestBody ...with@NotBlank,@Email,@Minand similar rules. - Collection body:
@RequestBody List<Student> studentsfor a JSON array. - Generic body:
@RequestBody Map<String, Object> datawhen there is no fixed structure. - Raw text body:
@RequestBody String body. - Content type:
consumes = "application/json"restricts what the endpoint accepts.
