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
- 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. - Install the Gradle build tool.
In macOS, this can be done with
brew install gradle. - Browse the spring initializr.
- For Project, select “Gradle - Kotlin”.
- For Language, select “Java”.
- For “Spring Boot”, select the latest version (for me this was “3.3.0”).
- For “Packaging”, select “Jar”.
- For “Java”, select the latest version (for me this was “21”).
- Click the “ADD DEPENDENCIES…” button.
- 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.
- Click the “GENERATE” button which
creates the file
demo.zipin the `Downloads’ directory. - Unzip that file and move the resulting directory to the desired location.
- Open a terminal and cd to the directory of the new project.
- Verify that the project builds by entering
./gradlew build
Implementing CRUD Endpoints
-
Open the project in your editor of choice.
-
Create the file
src/main/java/com/example/demo/DogController.javacontaining 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
-
Specify the port (defaults to 8080) to be used in
src/main/resources/application.propertiesby adding the following line:server.port=1919 -
Ensure that port to be used is not already in use.
-
Enter
./gradlew bootRun -
Browse
localhost:8080/api/dogand 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?