Spring Boot up and Running
Chap 1: in nutshell
3 foundational features:
- starters for dependency management
- Executable JARs for Simplified Deployment
- instead of shading (combining dependency + app JARs into one), nest JARs
- can run the app
- simple
java -jar <SpringBootAppName.jar> - make the Jar self-executable
- simple
- convention over configuration
- autoconfig
- defaults favor local dev environment
Chap 2: Choosing Tools and Getting Started
- Maven vs Gradle
- Maven:
- project structure strict convention
- rigic declarative -> consistency -> less issues
- project structure strict convention
- Gradle:
- Incremental compilation, only compile changes
- allows scripting/programming
- flexibility -> more tweaking and troubleshooting
- Maven:
- Java vs Kotlin
- Kotlin
- benefits:
- less verbose
- safe: by default no null values
- define own syntax with "extension function" and "infix" - more readable
- benefits:
- Kotlin
Chap 3: REST API
-
use https://httpie.io/ to test REST APIs (over Curl, postman)
-
Use class-level @RequestMapping (Private) annotation for shared URL portion
@RestController @RequestMapping("/coffees") class RestApiDemoController { -
Use ResponseEntity to include status codes.
@PutMapping("/{id}") ResponseEntity<Coffee> putCoffee(@PathVariable String id, @RequestBody Coffee coffee) { ... return (coffeeIndex == -1) ? new ResponseEntity<>(postCoffee(coffee), HttpStatus.CREATED) : new ResponseEntity<>(coffee, HttpStatus.OK); }
Chap 4: DB access
@entityon model, no-arg constructor, setters on all fields- Repository
CrudRepositorywith generic CRUD operationsJpaRepositoryextendsCrudRepository
Chap 5: Config
@ConfigurationProperties(prefix = "greeting")@ConfigurationPropertiesScan- need
spring-boot-configuration-processordependency - Autoconfiguration Report
java -jar bootapplication.jar --debugjava -Ddebug=true -jar bootapplication.jarexport DEBUG=truein shell- add
debug=truetoapplication.propertiesfile
- Actuator
- runtime info get/set
- need
spring-boot-starter-actuatordependency - secure by default with little info
- opening it up
management.endpoints.web.exposure.includemanagement.endpoint.health.show-details=alwaysorwhen_authorized
- query
/actuator/env- get env variable and where config props comes from/actuator/beans- all beans createdactuator/health- Health info (basic or expanded, depending on settings)/actuator/loggersecho '{"configuredLevel": "TRACE"}' | http :8080/actuator/loggers/org.springframework.data.webchange logger level on the fly
/actuator/mappings/actuator/heapdumpand/actuator/threaddump/actuator/metrics