Reputation: 1219
I am a beginner in spring boot application and I recently came across this project having following 3 packages
com.packagename.controller
com.packagname.domain
com.packagename.service
In domain package, there is a single class named Student having the following code
private String name;
public Student() {
}
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
Then in Service package, class StudentService
package com.javTpoint.service;
import org.springframework.stereotype.Service;
import com.javTpoint.domain.Student;
@Service
public class StudentService {
public Student saveStudent(Student student) {
student.setName(student.getName() + "123");
return student;
}
}
And finally in Controller package, Student class
package com.javTpoint.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.javTpoint.domain.Student;
import com.javTpoint.service.StudentService;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/student", method = RequestMethod.POST)
Student saveStuden(@RequestBody Student student) {
System.out.println(student);
return studentService.saveStudent(student);
}
}
The program asked to use this is Postman
I just want to understand the how the workflow is taking place like
why @RequestBody
is used inside saveStuden()
method in controller.
Upvotes: 2
Views: 5929
Reputation: 47905
When using Postman you likely invoke a URL such as http://<host>:<port>/student
using the HTTP POST
method to submit a payload (probably) expressed in JSON e.g.
{ "name": "aName" }
The flow from Postman goes like this:
POST
s the request body.http://<host>:<port>/student
consumes the request, deserialises the request body into a Student
object and delegates to an instance of StudentController
. This: @RequestBody Student student
instructs Spring to deserialise the given payload into an instance of Student
.StudentController
delegates to the instance of StudentService
which it has been injected with. Note: the The @Autowired
annotation instructs Spring's dependency injection mechanism to create an instance of StudentController
with an instance of StudentService
injected into it.StudentService
appends "123" to the student name and returns the mutated Student
instance.StudentController
returns the mutated Student
instance.Student
and returns the serialised representation to Postman.What you have described in your OP looks like a common division of responsibilities:
Update in response to this comment:
where should I learn what you know from? Any personal recommendations?
You could start with this Spring Boot example and walk through it step by step. Once you have it working (which typiclaly takes no more than 15 mins) then use ...
... to help you understand what Spring is doing 'under the covers'.
Upvotes: 2
Reputation: 182
The @RequestBody
annotation just before the student
parameter means the student
object will handle data you will send to /student
uri via a POST
request.
The data should be sent in JSON format:
for example:
{
name: "Joe Doe"
}
The studentService.saveStudent(student)
will alter the data of the student
object and the output would be.
{
name: "Joe Doe123"
}
Upvotes: 1