Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - @Service Annotation


What is the first thing that comes to your mind when you hear the term Service?


Well! It could be Service of any kind.


Say, the Service provided at a restaurant or the Service provided at some store.


Even in Spring Boot, Service is something of that kind.


In Spring Boot, Service classes are those where you are supposed to write business logic.


Let us say, you work in a company as a Manager. And your responsibility is to take the project from the client and distribute the work among the Engineers who works under you.


If you compare it with Spring Boot, you being a Manager is responsible to handle the request using @RestController and @RequestMapping annotation.


Once you have accepted the request, you are supposed to distribute the work to those Engineers working under you. And each engineer will design a Service for you.


Let us take a live example:


Say you are a Principal in a school. And the School Inspector has visited your school and asked you to give the list of students in class 5.


Now, you being an expert in Spring Boot, ask the class teacher of class 5, to give you the list of students in class 5.


Let's pause here! Think for a while!!


The class teacher of class 5 is given a task. In other words, he is asked to design a service.


And that is what he does.


At first he creates a class called Student.


Student.java


package org.example;


public class Student {


String name;
int roll;
int age;
int className;

//Getters, Setters and Constructors

}

Spring Boot - @Service Annotation

Then the class teacher prepares a java class named StudentService that has the details of all the students.


And now that we know that StudentService is the actual service prepared by the class teacher, he marks it with @Service annotation.


Note : @Service annotation tells Spring that the class is a Service.

Example :




import org.example.Student;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentService {

    public List studentDetails() {

        List studentList = new ArrayList<>();

        Student student1 = new Student("John", 1, 8, 5);
        Student student2 = new Student("Paul", 2, 7, 5);
        Student student3 = new Student("Andrew", 3, 8, 5);

        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);

        return studentList;
    }
}


Spring Boot - @Service Annotation

Just to keep things simple, let us say, there are only 3 students in the class.



Name Roll Age ClassName
John 1 8 5
Paul 2 7 5
Andrew 3 8 5

So, if you look at the above code, the class teacher prepares a List,


List studentList = new ArrayList<>();

And loads the list with three values,


Student student1 = new Student("John", 1, 8, 5);
Student student2 = new Student("Paul", 2, 7, 5);
Student student3 = new Student("Andrew", 3, 8, 5);

studentList.add(student1);
studentList.add(student2);
studentList.add(student3);

And returns the list loaded with student details.


And most importantly annotates the class StudentService with @Service annotation.


@Service
public class StudentService {
	...
}

Now, the class teacher has designed a Service StudentService and his job is over.


Now, you being the Principal of the school, your job is to make use of the Service.


And you have written the below code.


Example :



import org.example.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HelloWorldController {

    @Autowired
    StudentService studentService;

    @RequestMapping("/students")
    public List numberOfStudents() {

        return studentService.studentDetails();
    }
}



So as said, you being the Principal of the school, your job is to consume the Service.


And thanks to Spring, it made your life simpler with the @Autowired annotation.


@Autowired
StudentService studentService;

You consume the service using the @Autowired annotation.


So, your request looks like,


http://localhost:8080/students

And Spring Boot looks for @RestController, then it looks for @RequestMapping("/students"). Once it gets it calls the studentDetails() of the StudentService.


@RequestMapping("/students")
public List numberOfStudents() {

	return studentService.studentDetails();
}

And returns the student details in JSON format.


A bit confused? Where is studen details returned in JSON format?


Let's clear the confusion.


Let us start the Spring Boot Application

Spring Boot - @Service Annotation

Now, if we open the web browser and type the request


http://localhost:8080/students

We get the below response.

Spring Boot - @Service Annotation

And we can see that the response is in JSON format,


[{"name":"John","roll":1,"age":8,"className":5},{"name":"Paul","roll":2,"age":7,"className":5},{"name":"Andrew","roll":3,"age":8,"className":5}]

Long story short:

  1. You being the Principal of a school, is asked to give the list of the students of class 5.



    http://localhost:8080/students

  2. You being the Principal go to the class teacher of class 5 who provides the details of the students. Since the class teacher does the actual work, StudentService acts as a service and is annotated with @Service annotation.



    @Service
    public class StudentService {
    	public List studentDetails() {
    		List studentList = new ArrayList<>();
    		Student student1 = new Student("John", 1, 8, 5);
    		Student student2 = new Student("Paul", 2, 7, 5);
    		Student student3 = new Student("Andrew", 3, 8, 5);
    		studentList.add(student1);
    		studentList.add(student2);
    		studentList.add(student3);
    		return studentList;
    	}
    }

  3. Now, in the Controller, the you (The Principal) calls studentDetails() method of the Service StudentService and returns the details of the Students.



    @RestController

    public class HelloWorldController {
    
    	@Autowired
    	StudentService studentService;
    	@RequestMapping("/students")
    	public List numberOfStudents() {
    		return studentService.studentDetails();
    	}
    }