TIL: Spring Boot, pt4

Looks like there’ll be about one more day of working through this tutorial series. There as a lot of info covered today, and rather than trying to force myself to finish, I’m just going to call it quits at a good stopping point.

On top of all of the notes down below from today’s work, I also have a few other topics that I need to do some more reading on:

  1. method references
  2. lambda expressions
  3. the Optional Type

Anyways, here’s today’s notes.

Other Ways of Starting a Spring Boot App

  • There are other ways of starting a new Spring Boot app aside from the Maven method above

Spring Initializer

  • Located on the spring website: https://start.spring.io
  • Lets you select the type of project, as well as the version of spring boot that you want to use
  • Lets you supply the Group and Artifact for the project
  • Finally, allows you to supply the list of dependencies you want to add to the application
    • There’s a full list you can expand and select from, or type them in manually
  • Finally you can hit generate project, and it will create a link for you to download the generated project

Spring Boot CLI

  • Look on google for install link
  • Allows you to bootstrap a project from a Groovy template script
  • This is pretty much only used for quick prototyping, this isn’t something that you would use in a production environment.
  • Here’s a sample example of a hello-world rest api:
class ApplCtrl {

    String app() {
        "Hello World"
  • By running ./spring run hello.groovy, the CLI will create an entire application from it (will be up and running)

Via Spring Tool Suite IDE

  • IDE has an option to create a new Spring starter project
  • Gives you all of the same options that the initializer gives you
  • Will actually download the zip file from the start website and then import it for you automatically (just saves you one step of importing the project)

Customizing Spring Boot

  • This is when you need to change some parameters that aren’t satisfied by the default installation
  • This is done via a property file (the “easiest way”), which is spring related
    • application.properties
  • For example, let’s look at changing the default port that comes up at run time
  • Create the new file under the /src/main/resources directory
  • Inside the file, add server.port=8081
  • When you run the app again, it will start up on port 8081 instead
  • The properties that can go in this file can be found here

Connecting to a DB

  • This is done through the Spring JPA
    • Java Persistence API
    • Allows for Object - Relational Mapping

Setting up the Project

  • Create a new project using the initializr tool
    • Need the following dependencies: Web, JPA, Apache Derby (embedded db instance for prototyping)
  • In Intellij, go back to the main screen and click on import and then select the pom file
  • It will pull in the project and download everything that it needs

Switching from Static Data to DB

  • We first need to tell JPA that we have a data model that needs to be converted to a DB record
    • This is done by using the @Entity annotation on the model class
  • Need to give the JPA the primary key
    • Field level annotation @Id
  • Need to update our service so that it will connect over to the database instead of our premade data
  • This is handled by a new interface file which will allow us to extend a bunch of built in functionality from the CrudRepository class
    • Your basic get, post, update, delete functionality is already baked in, there’s no need to write these database calls yourself
    • Any calls that require more work (filtering) would just plug into the file
package io.springboot.topic;

import org.springframework.data.repository.CrudRepository;

public interface TopicRepository extends CrudRepository<Topic, String> {

  • Then in order to access this baked in functionality, we’ll wire in an instance of that interface file which will give us our functionality calls
private TopicRepository topicRepository;
Get All
  • In order to get all of the records from the database for the specific resource, we can run a .findAll() on the instance of our interface
    • Note that the findAll() will return an iterable
public List<Topic> getAllTopics() {
   List<Topic> topics = new ArrayList<>();
   return topics;
  • In order to add a record we can use the save method.
public void addTopic(Topic topic) {
Get One
  • The sample tutorial uses a method that’s now deprecated in later releases
  • To get a single record, you can use findById, but you also need to extend it with a orElse method, as it returns back an Optional
public Topic getTopic(String id) {
   return topicRepository.findById(id).orElse(null);
  • Just need to use the save method again. No difference between add and update
  • Use the deleteById method (video says delete, but that’s deprecated)
public void deleteTopic(String id) {