Shad
Shad

Reputation: 1219

What is the work flow of this Spring Boot program

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

Answers (2)

glytching
glytching

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:

  1. Postman POSTs the request body.
  2. The application which exposes the 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.
  3. The 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.
  4. The StudentService appends "123" to the student name and returns the mutated Student instance.
  5. The StudentController returns the mutated Student instance.
  6. The framework (i.e. Spring Boot) serialises the returned Student and returns the serialised representation to Postman.

What you have described in your OP looks like a common division of responsibilities:

  • The framework (Spring Boot in this case) provides the servlet container pieces and maps HTTP invocations to public methods on contoller classes
  • The controller provides the HTTP endpoint integration and is typically a lightweight facade over a service
  • The service is responsible for 'doing something' with the given object

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 ...

  • Logging
  • Debugging
  • Changing things (one thing at a time) and observing cause-and-effect

... to help you understand what Spring is doing 'under the covers'.

Upvotes: 2

sic-sic
sic-sic

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

Related Questions