66
Springboot from Newbie Perspective
Disclaimer: This is not a reference tutorial. At the time of writing I'm very inexperienced in SpringBoot or Java. Rather this is a written snapshot of me trying to understand and make sense of Springboot in early stages of Learning. So most of things explained here might seem super simple to a point where many would find them borderline incorrect. If you do so, I hope you've read this disclaimer and remembered it ;)
Until then, if you're curious as how a complete noob tries to make sense of springboot. how he maps different concepts in his mind etc.. whether the explanation that I give here makes sense from one noob to another (since at this point, I probably could emphathize with level of noob that seasoned springboot developers might struggle to, then read along :)
Note: If I used 'he' a lot, it's because I'm a he & I'm lazy to write he/she everywhere. Other than that, complete respect to all genders :)
Model
: User Model, with fields (handles model logics)Controller
: A REST endpoint that runs for a particular HTTP request (handles rest logic)Service
: An entity controller & DB (handles business logics)Dependency Injection
(idk what the heck it is really, but I know it's important concept to know in order to understand what springboot is...)Annotations
(there's annotations for everything, hopefully as we keep using them, it'll grow on us and soon we'd know whether something needs to be annotated with something or not)Access Modifiers & Privacy
, this is core java concept, but the most important one to be aware of (if you're also new to java)- Time, Currency, Locale
- Collections
Maven
(what it is, what is it's relationship to spring boot)@RestController
& optionally @RequestMapping
annotation@RestController
tells us this class has methods that act as a API endpoint handlers@RequestMapping
declares a base_url
for all the controller method endpoints@RestController
@RequestMapping("api/v1/student")
public class StudentController {
@Autowired
private StudentService studentService;
}
GetMapping(/user/{id})
@RequestBody
will map the post body to a POJO (I guess...)@PathVariable
will map a variable in path to a function param@PostMapping
public void addNewStudent(@RequestBody Student student) {
studentService.addNewStudent(student);
}
@Service
repository
(autowired), using @Autowired
annotationRepository
is an interface (explained next), which will extend a Parent repository class and thus would have implemented methods for basic query operations (such as findAll, findById etc..., for a particular model instance)autowired
)@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
/* methods */
}
Note: service is autowired
into controller, repository is autowired
into service.
@Autowired
public interface StudentRepository extends JpaRepository<Student, Long> {
Optional<Student> findStudentsByEmail(String email);
}
Note: We can add additional methods (apart from the methods declared in JpaRepository).
Class with @Entity
& @Table
annotation (from javax.persistence package (usually))
@Entity
tells us this class is POJO (Plain Old Java Objects) for a table
@Table
maps the class to a table (optinally can provide table name, usually inferred from className)
@Transient
for fields that are not a DB Column, but inferred from them (such as age
from DOB
, fullName
from firstName
& lastName
)
A logic to generate id
values (primary key)
@Entity
@Table
public class Student { /* ... */ }