Springboot from Newbie Perspective

When I first started learning springboot, it was kind of confusing, especially coming from JavaScript land, working on Node. First reason being, official docs is not very helpful.

I didn't know first thing about what is happening, what am I using for what components, is all dependencies installed? What inner tools are used within springboot. What's difference between spring & springboot. Should I learn both? Should I learn one? What's spring MVC in-between this?

I decided to stop thinking too much about these, at least in the beginning, and try to learn spring boot (because that's what is used in my company at present). This is part of series in my journey to learn spring boot. Today I went through a 1.5 hr video in YouTube with basic CRUD operations in SpringBoot with MySQL. So I'll cover what I learnt here. Hoping this would act as a bottom layer to build my view of spring boot on top of it.

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 :)

Problem we're trying to solve: (described in a framework agostic way) - A Basic CRUD setup (for 'user')

  • 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)

This does a nice separation of concerns for us.

So before I provide springboot solution for this common thing, we need to know what is unique about springboot

  • 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)
  • Some Imporant Java Util Packages
    • Time, Currency, Locale
    • Collections

Good to know

  • Maven (what it is, what is it's relationship to spring boot)

Controller

  • Class with @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

Eg:

@RestController
@RequestMapping("api/v1/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
}

Controller Method:

  • A method in controller class can be annotated to indicate which requests it handles (GET, PUT, POST, DELETE etc...)
  • GetMapping(/user/{id})

Method Param annotations:

  • @RequestBody will map the post body to a POJO (I guess...)
  • @PathVariable will map a variable in path to a function param

Eg:

@PostMapping
public void addNewStudent(@RequestBody Student student) {
    studentService.addNewStudent(student);
}

Service

  • Class annotated by @Service
  • Has corresponding repository (autowired), using @Autowired annotation
  • Repository 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)
  • the public methods declared here are usually invoked in controller methods (which as instance of service autowired)

Eg:

@Service
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    /* methods */
}

Note: service is autowired into controller, repository is autowired into service.

@Autowired

  • An annotation that tells spring to instantiate this class object automatically. (in order to do this, a default ctor should be present for the autowired class)

Repository

  • We pass in 2 generic params to Parent Repository (such as JPA Repository) which are: Model Class (explained next), Primary Key type

Eg:

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

Model

  • 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)

Eg:

@Entity
@Table
public class Student { /* ... */ }

49