Spring Boot Tutorial
Learn Spring Boot
Build production REST APIs with Spring Boot 4.1 and Spring Framework 7.
Stage 1
01
Getting Started
02
Core Spring
03
Configuration
Stage 2
04
Building REST Controllers
05
JSON with Jackson
06
Validation
07
Error Handling
Stage 3
08
Spring Data JPA
09
Transactions
10
JDBC and Migrations
11
Structuring an Application
Stage 4
12
Spring Security
13
JWT and OAuth2
14
Calling Other Services
Stage 5
15
Testing
16
Caching, Scheduling and Async
17
Messaging and Real-Time
Stage 6
18
Production Readiness
19
Performance and Scaling
Stage 7
20
Deploying Spring Boot
Spring Boot Reference
53 pages · 5 groups · syntax, parameters and worked examples
Core Annotations12
@SpringBootApplicationMarks the main class of a Spring Boot application: switches on auto-configuration and component scanning from the package it sits in.@ComponentMarks a class for component scanning so Spring creates one instance of it, injects its dependencies and makes it available to other beans.@ServiceA @Component specialisation for classes that hold business logic: the layer between controllers and repositories.@RepositoryMarks a data access class as a bean and turns database exceptions thrown from it into Spring's DataAccessException hierarchy.@ConfigurationDeclares a class whose @Bean methods define beans, with calls between those methods returning the shared instance by default.@BeanMarks a method whose return value Spring registers as a bean, for objects you cannot annotate or that need construction logic.@ValueInjects a single configuration value or expression into a constructor parameter, field or @Bean method parameter.@ConfigurationPropertiesBinds every property under a prefix to a typed object, usually a record, with relaxed names, conversion and optional validation.@ProfileRegisters a component, @Bean or configuration class only when one of the named profiles, or a profile expression, is active.@PrimaryMarks one bean as the default choice when several beans match an injection point, so plain injection by type is no longer ambiguous.@QualifierPicks a specific bean at an injection point by name or by a custom qualifier annotation when several beans share a type.@ConditionalOnPropertyRegisters a bean or configuration only when a configuration property is present, or has a specific value, so features can be switched per environment.
Web Annotations14
@RestControllerMarks a class as a web controller whose method return values are written straight to the response body, usually as JSON.@RequestMappingMaps requests to a controller class or method by path, HTTP method, parameters, headers, media types and API version.@GetMappingMaps HTTP GET requests to a handler method; use it for reads that never change server state.@PostMappingMaps HTTP POST requests to a handler method; use it to create resources and trigger actions.@PutMapping and @PatchMappingMap HTTP PUT (replace a whole resource) and PATCH (change some fields) requests to handler methods.@DeleteMappingMaps HTTP DELETE requests to a handler method; delete the resource and usually answer 204 No Content.@PathVariableBinds a {placeholder} from the request path, such as an id, to a method parameter with type conversion.@RequestParamBinds a query string or form parameter to a method parameter, with defaults, optional values and type conversion.@RequestBodyReads the request body, usually JSON, and converts it into a Java object such as a record DTO.@RequestHeaderBinds an HTTP request header, such as a tenant id or Accept-Language, to a method parameter.@ResponseStatusSets the HTTP status for a handler method's successful response, or for an exception class whenever it is thrown.@ExceptionHandlerMarks a method that turns exceptions thrown by handler methods into an HTTP response, such as a problem detail.@RestControllerAdviceDeclares a class whose @ExceptionHandler methods (and other advice) apply across many controllers, writing results as JSON.@CrossOriginAllows browsers on other origins to call a controller or method, by configuring CORS headers and preflight handling.
Data Annotations10
@EntityMarks a class as a JPA entity: Hibernate maps it to a table, tracks its changes and lets you query it by its entity name in JPQL.@Id and @GeneratedValue@Id marks an entity's primary key; @GeneratedValue lets the database or Hibernate assign it using IDENTITY, SEQUENCE, UUID or AUTO.@ColumnCustomises how an entity field maps to its column: name, nullability, length, precision, uniqueness, and whether Hibernate includes it in INSERT and UPDATE.@ManyToOneMaps a foreign key to a reference to another entity, such as an order's customer; it loads eagerly by default, so set fetch to LAZY.@OneToManyMaps a parent's collection of children, such as an order's lines; use mappedBy to point at the child's @ManyToOne and keep both sides in sync.@TransactionalRuns a method in a database transaction through a Spring proxy: commit on success, roll back on unchecked exceptions, plus propagation and read-only options.@QueryDeclares the JPQL or native SQL a Spring Data repository method runs, with named parameters, paging count queries and projections.@ModifyingMarks a @Query as an UPDATE, DELETE or INSERT so Spring Data executes it as a bulk statement and returns the affected row count.@VersionAdds optimistic locking: Hibernate checks and increments a version column on every update, so concurrent edits fail instead of overwriting each other.@CreatedDate and @LastModifiedDateSpring Data auditing fills these fields with the creation and last update time automatically, once auditing is enabled and the entity listener is registered.
Testing Annotations7
@SpringBootTestStarts the whole application context for a test, optionally with a real server on a random port, so you test the app as it runs.@WebMvcTestStarts only the Spring MVC layer for chosen controllers, with MockMvcTester ready, so controller tests run fast without services or a database.@DataJpaTestStarts only JPA, your repositories and a test database, wrapping each test in a transaction that rolls back, to test queries and mappings.@MockitoBeanReplaces a bean in the test's application context with a Mockito mock, so a test controls what a collaborator returns and verifies what was called.@ServiceConnectionMarks a Testcontainers container so Spring Boot connects the application to it automatically, with no URLs, ports or passwords in test properties.@AutoConfigureMockMvcRegisters MockMvc and MockMvcTester in a @SpringBootTest, so requests run through the full application without starting a server.@SqlRuns SQL scripts or inline statements against the test DataSource before or after a test method or class, to set up and clean up exactly the rows a test needs.
Common Properties10
server.portSets the HTTP port the embedded server listens on: 8080 by default, 0 for a random free port, -1 to start no server at all.spring.datasource.urlThe JDBC URL of the application's database; Boot builds a pooled DataSource from it and infers the driver from the URL.spring.jpa.hibernate.ddl-autoControls whether Hibernate creates, updates, validates or leaves alone the database schema at startup; use validate or none in production.spring.jpa.open-in-viewKeeps a JPA EntityManager open for the whole web request; it is on by default, and REST APIs are usually better off turning it off.spring.profiles.activeLists the profiles that are active, which decides which profile-specific files and @Profile beans load; set it per deployment, not in the jar.logging.levelSets log levels per logger or package (logging.level.<name>=DEBUG), with groups such as web and sql to switch related loggers together.management.endpoints.web.exposure.includeChooses which Actuator endpoints are published over HTTP; only health is exposed by default, and * exposes everything.spring.threads.virtual.enabledSwitches the embedded server, task executor and scheduler to Java virtual threads, so blocking code scales without large thread pools.spring.mvc.problemdetails.enabledMakes Spring MVC's own exceptions return RFC 9457 Problem Details JSON (application/problem+json); it is off by default.server.shutdownChooses between graceful shutdown, which lets in-flight requests finish, and immediate shutdown; graceful is the default in Spring Boot 4.1.