Spring Boot Reference Guide

Authors

Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, Andy Wilkinson, Marcel Overdijk, Christian Dupuis, Sébastien Deleuze, Michael Simons, Vedran Pavić, Jay Bryant

2.0.0.M7

Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.


Table of Contents

I. Spring Boot Documentation
1. About the Documentation
2. Getting Help
3. First Steps
4. Working with Spring Boot
5. Learning about Spring Boot Features
6. Moving to Production
7. Advanced Topics
II. Getting Started
8. Introducing Spring Boot
9. System Requirements
9.1. Servlet Containers
10. Installing Spring Boot
10.1. Installation Instructions for the Java Developer
10.1.1. Maven Installation
10.1.2. Gradle Installation
10.2. Installing the Spring Boot CLI
10.2.1. Manual Installation
10.2.2. Installation with SDKMAN!
10.2.3. OSX Homebrew Installation
10.2.4. MacPorts Installation
10.2.5. Command-line Completion
10.2.6. Quick-start Spring CLI Example
10.3. Upgrading from an Earlier Version of Spring Boot
11. Developing Your First Spring Boot Application
11.1. Creating the POM
11.2. Adding Classpath Dependencies
11.3. Writing the Code
11.3.1. The @RestController and @RequestMapping Annotations
11.3.2. The @EnableAutoConfiguration Annotation
11.3.3. The “main” Method
11.4. Running the Example
11.5. Creating an Executable Jar
12. What to Read Next
III. Using Spring Boot
13. Build Systems
13.1. Dependency Management
13.2. Maven
13.2.1. Inheriting the Starter Parent
13.2.2. Using Spring Boot without the Parent POM
13.2.3. Using the Spring Boot Maven Plugin
13.3. Gradle
13.4. Ant
13.5. Starters
14. Structuring Your Code
14.1. Using the “default” Package
14.2. Locating the Main Application Class
15. Configuration Classes
15.1. Importing Additional Configuration Classes
15.2. Importing XML Configuration
16. Auto-configuration
16.1. Gradually Replacing Auto-configuration
16.2. Disabling Specific Auto-configuration Classes
17. Spring Beans and Dependency Injection
18. Using the @SpringBootApplication Annotation
19. Running Your Application
19.1. Running from an IDE
19.2. Running as a Packaged Application
19.3. Using the Maven Plugin
19.4. Using the Gradle Plugin
19.5. Hot Swapping
20. Developer Tools
20.1. Property Defaults
20.2. Automatic Restart
20.2.1. Logging changes in condition evaluation
20.2.2. Excluding Resources
20.2.3. Watching Additional Paths
20.2.4. Disabling Restart
20.2.5. Using a Trigger File
20.2.6. Customizing the Restart Classloader
20.2.7. Known Limitations
20.3. LiveReload
20.4. Global Settings
20.5. Remote Applications
20.5.1. Running the Remote Client Application
20.5.2. Remote Update
21. Packaging Your Application for Production
22. What to Read Next
IV. Spring Boot features
23. SpringApplication
23.1. Startup Failure
23.2. Customizing the Banner
23.3. Customizing SpringApplication
23.4. Fluent Builder API
23.5. Application Events and Listeners
23.6. Web Environment
23.7. Accessing Application Arguments
23.8. Using the ApplicationRunner or CommandLineRunner
23.9. Application Exit
23.10. Admin Features
24. Externalized Configuration
24.1. Configuring Random Values
24.2. Accessing Command Line Properties
24.3. Application Property Files
24.4. Profile-specific Properties
24.5. Placeholders in Properties
24.6. Using YAML Instead of Properties
24.6.1. Loading YAML
24.6.2. Exposing YAML as Properties in the Spring Environment
24.6.3. Multi-profile YAML Documents
24.6.4. YAML Shortcomings
24.6.5. Merging YAML Lists
24.7. Type-safe Configuration Properties
24.7.1. Third-party Configuration
24.7.2. Relaxed Binding
24.7.3. Properties Conversion
24.7.4. @ConfigurationProperties Validation
24.7.5. @ConfigurationProperties vs. @Value
25. Profiles
25.1. Adding Active Profiles
25.2. Programmatically Setting Profiles
25.3. Profile-specific Configuration Files
26. Logging
26.1. Log Format
26.2. Console Output
26.2.1. Color-coded Output
26.3. File Output
26.4. Log Levels
26.5. Custom Log Configuration
26.6. Logback Extensions
26.6.1. Profile-specific Configuration
26.6.2. Environment Properties
27. Developing Web Applications
27.1. The ‘Spring Web MVC Framework’
27.1.1. Spring MVC Auto-configuration
27.1.2. HttpMessageConverters
27.1.3. Custom JSON Serializers and Deserializers
27.1.4. MessageCodesResolver
27.1.5. Static Content
27.1.6. Welcome Page
27.1.7. Custom Favicon
27.1.8. ConfigurableWebBindingInitializer
27.1.9. Template Engines
27.1.10. Error Handling
Custom Error Pages
Mapping Error Pages outside of Spring MVC
Error Handling on WebSphere Application Server
27.1.11. Spring HATEOAS
27.1.12. CORS Support
27.2. The ‘Spring WebFlux Framework’
27.2.1. Spring WebFlux Auto-configuration
27.2.2. HTTP Codecs with HttpMessageReaders and HttpMessageWriters
27.2.3. Static Content
27.2.4. Template Engines
27.2.5. Error Handling
Custom Error Pages
27.3. JAX-RS and Jersey
27.4. Embedded Servlet Container Support
27.4.1. Servlets, Filters, and listeners
Registering Servlets, Filters, and Listeners as Spring Beans
27.4.2. Servlet Context Initialization
Scanning for Servlets, Filters, and listeners
27.4.3. The ServletWebServerApplicationContext
27.4.4. Customizing Embedded Servlet Containers
Programmatic Customization
Customizing ConfigurableServletWebServerFactory Directly
27.4.5. JSP Limitations
28. Security
28.1. OAuth2
28.1.1. Client
28.2. Actuator Security
29. Working with SQL Databases
29.1. Configure a DataSource
29.1.1. Embedded Database Support
29.1.2. Connection to a Production Database
29.1.3. Connection to a JNDI DataSource
29.2. Using JdbcTemplate
29.3. JPA and ‘Spring Data’
29.3.1. Entity Classes
29.3.2. Spring Data JPA Repositories
29.3.3. Creating and Dropping JPA Databases
29.3.4. Open EntityManager in View
29.4. Using H2’s Web Console
29.4.1. Changing the H2 Console’s Path
29.5. Using jOOQ
29.5.1. Code Generation
29.5.2. Using DSLContext
29.5.3. jOOQ SQL Dialect
29.5.4. Customizing jOOQ
30. Working with NoSQL Technologies
30.1. Redis
30.1.1. Connecting to Redis
30.2. MongoDB
30.2.1. Connecting to a MongoDB Database
30.2.2. MongoTemplate
30.2.3. Spring Data MongoDB Repositories
30.2.4. Embedded Mongo
30.3. Neo4j
30.3.1. Connecting to a Neo4j Database
30.3.2. Using the Embedded Mode
30.3.3. Neo4jSession
30.3.4. Spring Data Neo4j Repositories
30.3.5. Repository Example
30.4. Gemfire
30.5. Solr
30.5.1. Connecting to Solr
30.5.2. Spring Data Solr Repositories
30.6. Elasticsearch
30.6.1. Connecting to Elasticsearch by Using Jest
30.6.2. Connecting to Elasticsearch by Using Spring Data
30.6.3. Spring Data Elasticsearch Repositories
30.7. Cassandra
30.7.1. Connecting to Cassandra
30.7.2. Spring Data Cassandra Repositories
30.8. Couchbase
30.8.1. Connecting to Couchbase
30.8.2. Spring Data Couchbase Repositories
30.9. LDAP
30.9.1. Connecting to an LDAP Server
30.9.2. Spring Data LDAP Repositories
30.9.3. Embedded In-memory LDAP Server
30.10. InfluxDB
30.10.1. Connecting to InfluxDB
31. Caching
31.1. Supported Cache Providers
31.1.1. Generic
31.1.2. JCache (JSR-107)
31.1.3. EhCache 2.x
31.1.4. Hazelcast
31.1.5. Infinispan
31.1.6. Couchbase
31.1.7. Redis
31.1.8. Caffeine
31.1.9. Simple
31.1.10. None
32. Messaging
32.1. JMS
32.1.1. ActiveMQ Support
32.1.2. Artemis Support
32.1.3. Using a JNDI ConnectionFactory
32.1.4. Sending a Message
32.1.5. Receiving a Message
32.2. AMQP
32.2.1. RabbitMQ support
32.2.2. Sending a Message
32.2.3. Receiving a Message
32.3. Apache Kafka Support
32.3.1. Sending a Message
32.3.2. Receiving a Message
32.3.3. Additional Kafka Properties
33. Calling REST Services with ‘RestTemplate’
33.1. RestTemplate Customization
34. Calling REST Services with ‘WebClient’
34.1. WebClient Customization
35. Validation
36. Sending Email
37. Distributed Transactions with JTA
37.1. Using an Atomikos Transaction Manager
37.2. Using a Bitronix Transaction Manager
37.3. Using a Narayana Transaction Manager
37.4. Using a Java EE Managed Transaction Manager
37.5. Mixing XA and Non-XA JMS Connections
37.6. Supporting an Alternative Embedded Transaction Manager
38. Hazelcast
39. Quartz Scheduler
40. Spring Integration
41. Spring Session
42. Monitoring and Management over JMX
43. Testing
43.1. Test Scope Dependencies
43.2. Testing Spring Applications
43.3. Testing Spring Boot Applications
43.3.1. Detecting Test Configuration
43.3.2. Excluding Test Configuration
43.3.3. Working with Random Ports
43.3.4. Mocking and Spying Beans
43.3.5. Auto-configured Tests
43.3.6. Auto-configured JSON Tests
43.3.7. Auto-configured Spring MVC Tests
43.3.8. Auto-configured Spring WebFlux Tests
43.3.9. Auto-configured Data JPA Tests
43.3.10. Auto-configured JDBC Tests
43.3.11. Auto-configured jOOQ Tests
43.3.12. Auto-configured Data MongoDB Tests
43.3.13. Auto-configured Data Neo4j Tests
43.3.14. Auto-configured Data Redis Tests
43.3.15. Auto-configured Data LDAP Tests
43.3.16. Auto-configured REST Clients
43.3.17. Auto-configured Spring REST Docs Tests
Auto-configured Spring REST Docs Tests with Mock MVC
Auto-configured Spring REST Docs Tests with REST Assured
43.3.18. User Configuration and Slicing
43.3.19. Using Spock to Test Spring Boot Applications
43.4. Test Utilities
43.4.1. ConfigFileApplicationContextInitializer
43.4.2. EnvironmentTestUtils
43.4.3. OutputCapture
43.4.4. TestRestTemplate
44. WebSockets
45. Web Services
46. Creating Your Own Auto-configuration
46.1. Understanding Auto-configured Beans
46.2. Locating Auto-configuration Candidates
46.3. Condition Annotations
46.3.1. Class Conditions
46.3.2. Bean Conditions
46.3.3. Property Conditions
46.3.4. Resource Conditions
46.3.5. Web Application Conditions
46.3.6. SpEL Expression Conditions
46.4. Creating Your Own Starter
46.4.1. Naming
46.4.2. autoconfigure Module
46.4.3. Starter Module
47. What to Read Next
V. Spring Boot Actuator: Production-ready features
48. Enabling Production-ready Features
49. Endpoints
49.1. Exposing Endpoints
49.2. Securing HTTP Endpoints
49.3. Customizing Endpoints
49.4. Hypermedia for Actuator Web Endpoints
49.5. Actuator Web Endpoint Paths
49.6. CORS Support
49.7. Adding Custom Endpoints
49.8. Health Information
49.8.1. Auto-configured HealthIndicators
49.8.2. Writing Custom HealthIndicators
49.8.3. Reactive Health Indicators
49.8.4. Auto-configured ReactiveHealthIndicators
49.9. Application Information
49.9.1. Auto-configured InfoContributors
49.9.2. Custom Application Information
49.9.3. Git Commit Information
49.9.4. Build Information
49.9.5. Writing Custom InfoContributors
50. Monitoring and Management over HTTP
50.1. Customizing the Management Endpoint Paths
50.2. Customizing the Management Server Port
50.3. Configuring Management-specific SSL
50.4. Customizing the Management Server Address
50.5. Disabling HTTP Endpoints
51. Monitoring and Management over JMX
51.1. Customizing MBean Names
51.2. Disabling JMX Endpoints
51.3. Using Jolokia for JMX over HTTP
51.3.1. Customizing Jolokia
51.3.2. Disabling Jolokia
52. Loggers
52.1. Configure a Logger
53. Metrics
53.1. Spring MVC Metrics
53.1.1. Spring MVC Metric Tags
53.2. WebFlux Metrics
53.2.1. WebFlux Metric Tags
53.3. RestTemplate Metrics
53.3.1. RestTemplate Metric Tags
53.4. DataSource metrics
53.5. Spring Integration Metrics
54. Auditing
55. Tracing
55.1. Custom tracing
56. Process Monitoring
56.1. Extend Configuration
56.2. Programmatically
57. Cloud Foundry Support
57.1. Disabling Extended Cloud Foundry Actuator Support
57.2. Cloud Foundry Self-signed Certificates
57.3. Custom Security Configuration
58. What to Read Next
VI. Deploying Spring Boot Applications
59. Deploying to the Cloud
59.1. Cloud Foundry
59.1.1. Binding to Services
59.2. Heroku
59.3. OpenShift
59.4. Amazon Web Services (AWS)
59.4.1. AWS Elastic Beanstalk
Using the Tomcat Platform
Using the Java SE Platform
59.4.2. Summary
59.5. Boxfuse and Amazon Web Services
59.6. Google Cloud
60. Installing Spring Boot Applications
60.1. Supported Operating Systems
60.2. Unix/Linux Services
60.2.1. Installation as an init.d Service (System V)
Securing an init.d Service
60.2.2. Installation as a systemd Service
60.2.3. Customizing the Startup Script
Customizing the Start Script when It Is Written
Customizing a Script When It Runs
60.3. Microsoft Windows Services
61. What to Read Next
VII. Spring Boot CLI
62. Installing the CLI
63. Using the CLI
63.1. Running Applications with the CLI
63.1.1. Deduced “grab” Dependencies
63.1.2. Deduced “grab” Coordinates
63.1.3. Default Import Statements
63.1.4. Automatic Main Method
63.1.5. Custom Dependency Management
63.2. Applications with Multiple Source Files
63.3. Packaging Your Application
63.4. Initialize a New Project
63.5. Using the Embedded Shell
63.6. Adding Extensions to the CLI
64. Developing Applications with the Groovy Beans DSL
65. Configuring the CLI with settings.xml
66. What to Read Next
VIII. Build tool plugins
67. Spring Boot Maven Plugin
67.1. Including the Plugin
67.2. Packaging Executable Jar and War Files
68. Spring Boot Gradle Plugin
69. Spring Boot AntLib Module
69.1. Spring Boot Ant Tasks
69.1.1. spring-boot:exejar
69.1.2. Examples
69.2. spring-boot:findmainclass
69.2.1. Examples
70. Supporting Other Build Systems
70.1. Repackaging Archives
70.2. Nested Libraries
70.3. Finding a Main Class
70.4. Example Repackage Implementation
71. What to Read Next
IX. ‘How-to’ guides
72. Spring Boot Application
72.1. Create Your Own FailureAnalyzer
72.2. Troubleshoot Auto-configuration
72.3. Customize the Environment or ApplicationContext Before It Starts
72.4. Build an ApplicationContext Hierarchy (Adding a Parent or Root Context)
72.5. Create a Non-web Application
73. Properties and Configuration
73.1. Automatically Expand Properties at Build Time
73.1.1. Automatic Property Expansion Using Maven
73.1.2. Automatic Property Expansion Using Gradle
73.2. Externalize the Configuration of SpringApplication
73.3. Change the Location of External Properties of an Application
73.4. Use ‘Short’ Command Line Arguments
73.5. Use YAML for External Properties
73.6. Set the Active Spring Profiles
73.7. Change Configuration Depending on the Environment
73.8. Discover Built-in Options for External Properties
74. Embedded Web Servers
74.1. Use Another Web Server
74.2. Configure Jetty
74.3. Add a Servlet, Filter, or Listener to an Application
74.3.1. Add a Servlet, Filter, or Listener by Using a Spring Bean
Disable Registration of a Servlet or Filter
74.3.2. Add Servlets, Filters, and Listeners by Using Classpath Scanning
74.4. Change the HTTP Port
74.5. Use a Random Unassigned HTTP Port
74.6. Discover the HTTP Port at Runtime
74.7. Configure SSL
74.8. Configure HTTP/2
74.8.1. HTTP/2 with Undertow
74.8.2. HTTP/2 with Tomcat
74.9. Configure Access Logging
74.10. Running Behind a Front-end Proxy Server
74.10.1. Customize Tomcat’s Proxy Configuration
74.11. Configure Tomcat
74.12. Enable Multiple Connectors with Tomcat
74.13. Use Tomcat’s LegacyCookieProcessor
74.14. Configure Undertow
74.15. Enable Multiple Listeners with Undertow
74.16. Create WebSocket Endpoints Using @ServerEndpoint
74.17. Enable HTTP Response Compression
75. Spring MVC
75.1. Write a JSON REST Service
75.2. Write an XML REST Service
75.3. Customize the Jackson ObjectMapper
75.4. Customize the @ResponseBody Rendering
75.5. Handling Multipart File Uploads
75.6. Switch Off the Spring MVC DispatcherServlet
75.7. Switch off the Default MVC Configuration
75.8. Customize ViewResolvers
76. HTTP Clients
76.1. Configure RestTemplate to Use a Proxy
77. Logging
77.1. Configure Logback for Logging
77.1.1. Configure Logback for File-only Output
77.2. Configure Log4j for Logging
77.2.1. Use YAML or JSON to Configure Log4j 2
78. Data Access
78.1. Configure a Custom DataSource
78.2. Configure Two DataSources
78.3. Use Spring Data Repositories
78.4. Separate @Entity Definitions from Spring Configuration
78.5. Configure JPA Properties
78.6. Configure Hibernate Naming Strategy
78.7. Use a Custom EntityManagerFactory
78.8. Use Two EntityManagers
78.9. Use a Traditional persistence.xml File
78.10. Use Spring Data JPA and Mongo Repositories
78.11. Expose Spring Data Repositories as REST Endpoint
78.12. Configure a Component that is Used by JPA
79. Database Initialization
79.1. Initialize a Database Using JPA
79.2. Initialize a Database Using Hibernate
79.3. Initialize a Database
79.4. Initialize a Spring Batch Database
79.5. Use a Higher-level Database Migration Tool
79.5.1. Execute Flyway Database Migrations on Startup
79.5.2. Execute Liquibase Database Migrations on Startup
80. Messaging
80.1. Disable Transacted JMS Session
81. Batch Applications
81.1. Execute Spring Batch Jobs on Startup
82. Actuator
82.1. Change the HTTP Port or Address of the Actuator Endpoints
82.2. Customize the ‘whitelabel’ Error Page
83. Security
83.1. Switch off the Spring Boot Security Configuration
83.2. Change the AuthenticationManager and Add User Accounts
83.3. Enable HTTPS When Running behind a Proxy Server
84. Hot Swapping
84.1. Reload Static Content
84.2. Reload Templates without Restarting the Container
84.2.1. Thymeleaf Templates
84.2.2. FreeMarker Templates
84.2.3. Groovy Templates
84.3. Fast Application Restarts
84.4. Reload Java Classes without Restarting the Container
85. Build
85.1. Generate Build Information
85.2. Generate Git Information
85.3. Customize Dependency Versions
85.4. Create an Executable JAR with Maven
85.5. Use a Spring Boot Application as a Dependency
85.6. Extract Specific Libraries When an Executable Jar Runs
85.7. Create a Non-executable JAR with Exclusions
85.8. Remote Debug a Spring Boot Application Started with Maven
85.9. Build an Executable Archive from Ant without Using spring-boot-antlib
86. Traditional Deployment
86.1. Create a Deployable War File
86.2. Create a Deployable War File for Older Servlet Containers
86.3. Convert an Existing Application to Spring Boot
86.4. Deploying a WAR to WebLogic
86.5. Deploying a WAR in an Old (Servlet 2.5) Container
86.6. Use Jedis Instead of Lettuce
X. Appendices
A. Common application properties
B. Configuration Metadata
B.1. Metadata Format
B.1.1. Group Attributes
B.1.2. Property Attributes
B.1.3. Hint Attributes
B.1.4. Repeated Metadata Items
B.2. Providing Manual Hints
B.2.1. Value Hint
B.2.2. Value Providers
Any
Class Reference
Handle As
Logger Name
Spring Bean Reference
Spring Profile Name
B.3. Generating Your Own Metadata by Using the Annotation Processor
B.3.1. Nested Properties
B.3.2. Adding Additional Metadata
C. Auto-configuration classes
C.1. From the “spring-boot-autoconfigure” module
C.2. From the “spring-boot-actuator-autoconfigure” module
D. Test auto-configuration annotations
E. The Executable Jar Format
E.1. Nested JARs
E.1.1. The Executable Jar File Structure
E.1.2. The Executable War File Structure
E.2. Spring Boot’s “JarFile” Class
E.2.1. Compatibility with the Standard Java “JarFile”
E.3. Launching Executable Jars
E.3.1. Launcher Manifest
E.3.2. Exploded Archives
E.4. PropertiesLauncher Features
E.5. Executable Jar Restrictions
E.6. Alternative Single Jar Solutions
F. Dependency versions