Spring Boot
Note: Spring Boot - Version 2.4
General
- From Spring 4.3 onward, a class with a single constructor does not require the annotation for autowired parameters.
Web
- Class
@RestController@RequestMapping("/PATH")
- Method
@GetMapping("/PATH"),@PostMapping("/PATH")- Path Var
1 2 3
@GetMapping("/PATH/{VAR1}/{VAR2}") public ResponseEntity<?> get(@PathVariable("VAR1") String v1, @PathVariable("VAR2") String v2) { }
Concurrency
Scheduling
@EnableSchedulingprivate final TaskScheduler taskScheduler- Inject Scheduler@Scheduled(initialDelayString="", fixedDelayString="")- On Method(s)
Custom Properties
- Add dependency
- The class
1 2 3 4 5
@Component @ConfigurationProperties(prefix = "my.prefix") public class TheProperties { // ... }
- In case of a simple POJO to be used as configuration (e.g. third-party code), define a method like the following one
1 2 3 4 5 6 7 8 9 10
@SpringBootApplication public class MyApp { // ... @Bean @ConfigurationProperties(prefix = "my.prefix") public TheProperties createProperties() { return new TheProperties(); } }
Note: use @Component over each class or apply @ConfigurationPropertiesScan to scan base package.
Configuration
- Externalized Configuration
- Enable autoconfiguration report, conditions evaluation report consists of multiple sections
such as positive matches, negative matches, exclusions, unconditional classes, etc via
java -jar APP.jar --debugjava -Ddebug=true -jar APP.jardebug=true- inapplication.propertiesexport DEBUG=true- as OS env
pom.xml
1
2
3
<properties>
<java.version>11</java.version>
</properties>
application.yml- Simple Sample
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server:
shutdown: graceful
spring:
profiles:
active: prod
lifecycle:
timeout-per-shutdown-phase: 20s
---
spring:
config:
activate:
on-profile: dev
logging:
level:
org.x.y.z: debug
Actuator
- Add dependency
- Define policy on
/actuator/**if security is enabled management.endpoints.web.exposure.include=*- show all endpoints- Instead of
*, a comma-separated list can be used such asenv- environment variablesconfigprops- config propertiesmetrics- some components specific metrics such asjvm,hikaricp,tomcat, …mappings- list all request endpoints mappings to associated controller classesheapdumpthreaddump
management.endpoint.health.show-details=never|when_authorized|always- show detailed health info such asdb,diskSpace, …
Security
- Disable default security password in case of applying other options such as JWT [REF]
1 2 3 4
@SpringBootApplication(exclude = {UserDetailsServiceAutoConfiguration.class}) public class MyApp { // ... }