The Question Mark - blog by Mark Volkmann

Spring Boot

Spring logo

Overview

Spring is a set of Java services that aim to simplify some common tasks. Spring Boot “makes it easy to create stand-alone, production-grade Spring based Applications”.

VS Code

When using VS Code for Java development:

  • Install the extension “Extension Pack For Java” from Microsoft.
  • If using Spring Boot, install the “Spring Boot Extension Pack” from VMware.

Creating a Project

  1. Install the latest version of the Java Development Kit (JDK). When using SDKMAN, change the version of Java used with sdk use java 21.0.3-tem.
  2. Install the Gradle build tool. In macOS, this can be done with brew install gradle.
  3. Browse the spring initializr.
  4. For Project, select “Gradle - Kotlin”.
  5. For Language, select “Java”.
  6. For “Spring Boot”, select the latest version (for me this was “3.3.0”).
  7. For “Packaging”, select “Jar”.
  8. For “Java”, select the latest version (for me this was “21”).
  9. Click the “ADD DEPENDENCIES…” button.
  10. Under the “WEB” category, click “Spring Web” and “Spring Boot DevTools”. “Spring Boot DevTools” can automatically restart the Spring server when code changes are detected.
  11. Click the “GENERATE” button which creates the file demo.zip in the `Downloads’ directory.
  12. Unzip that file and move the resulting directory to the desired location.
  13. Open a terminal and cd to the directory of the new project.
  14. Verify that the project builds by entering ./gradlew build

Implementing CRUD Endpoints

  1. Open the project in your editor of choice.

  2. Create the file src/main/java/com/example/demo/DogController.java containing the following which implements endpoints for all the CRUD operations:

    package com.example.demo;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.UUID;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.\*;
    
    @RestController
    public class DogController {
    
        HashMap<UUID, Dog> dogMap = new HashMap<>();
    
        public DogController() {
            addDog("Whippet", "Comet");
            addDog("German Shorthaired Pointer", "Oscar");
        }
    
        private Dog addDog(String breed, String name) {
            Dog dog = new Dog(breed, name);
            dogMap.put(dog.id, dog);
            return dog;
        }
    
        @GetMapping("/api/dog")
        public Dog[] getDogs() {
            System.out.println("in getDogs");
            Collection<Dog> dogCollection = dogMap.values();
            Dog[] dogArray = dogCollection.toArray(new Dog[0]);
            return dogArray;
        }
    
        @PostMapping("/api/dog")
        public Dog createDog(@RequestBody Dog dog) {
            return addDog(dog.breed, dog.name);
        }
    
        @PutMapping("/api/dog/{dogId}")
        public Dog updateDog(@PathVariable String dogId, @RequestBody Dog dog) {
            Dog existingDog = dogMap.get(UUID.fromString(dogId));
            existingDog.breed = dog.breed;
            existingDog.name = dog.name;
            return existingDog;
        }
    
        @DeleteMapping("/api/dog/{dogId}")
        public ResponseEntity<Void> deleteDog(@PathVariable String dogId) {
            Dog dog = dogMap.remove(UUID.fromString(dogId));
            return new ResponseEntity<>(null, dog == null ? HttpStatus.NOT_FOUND : HttpStatus.OK);
        }
    }
    

Running the Server

  1. Specify the port (defaults to 8080) to be used in src/main/resources/application.properties by adding the following line:

    server.port=1919
    
  2. Ensure that port to be used is not already in use.

  3. Enter ./gradlew bootRun

  4. Browse localhost:8080/api/dog and verify that a JSON array of dog descriptions is output.

Reducing Terminal Logging

To reduce the amount of logging output in the terminal where the server is started, add the following in resources/application.yaml:

logging.level:
  root: error
  com.example.demo: debug

Automatic Server Restart

The server can be configured to automatically restart when any source files are modified. To configure this, add the following line in src/main/resources/application.properties:

spring.devtools.restart.additional-paths=src/main/**

TODO: Why doesn’t this work?

Implementing Unit Tests