Contents
Spring Boot
The main Java framework for server applications: REST APIs, Spring Data, security, testing and deployment.
1.Getting started
- What Spring and Spring Boot areWhy Spring exists, how Spring Boot differs from it and what we will build in this course.
- Creating a projectHow to create a Spring Boot project on start.spring.io and what each generated file is for.
- Project structure and pom.xmlWhere code, settings and templates live in a Spring Boot project, and how pom.xml with starters works.
- Running and the embedded serverHow to start a Spring Boot application, what the log lines mean and how to change the embedded Tomcat port.
2.Spring basics
- Beans and the containerWhat a Spring bean is, how @Component and @Service register classes and what @Bean is for.
- Dependency injectionHow Spring passes beans into other beans through the constructor and what to do when several beans match.
- Settings: properties and YAMLHow to set values in application.properties and application.yaml, read them with @Value and override them at startup.
- ProfilesHow to keep different settings for development and the server with Spring profiles and how to switch them on.
- A group of settings in a recordHow to collect related settings into one record with @ConfigurationProperties instead of scattered @Value.
3.REST API
- A controller and JSONHow @RestController accepts HTTP requests and turns returned objects into JSON.
- Addresses, path variables and query parametersHow to set a common address prefix, take a post id from the path with @PathVariable and a filter with @RequestParam.
- Request body and DTOsHow to accept JSON in a POST request with @RequestBody and why input data gets its own record.
- Responses and status codesHow to return the right HTTP status code with ResponseEntity and @ResponseStatus, and how updating and deleting work.
- Validating dataHow to check incoming data with Jakarta Validation annotations and @Valid without writing a single if.
- Error handling and ProblemDetailHow to turn exceptions into clear API responses with @RestControllerAdvice and the standard ProblemDetail format.
- Services and layersHow to split an application into controllers, services and repositories and what code goes into which layer.
- Uploading filesHow to accept a file in a request with MultipartFile, limit its size, save it to disk and serve it back at an address.
- CORS: requests from another siteWhy the browser blocks API requests from a page at another address and how to allow them with @CrossOrigin and a global setting, including with Spring Security.
- API documentation: OpenAPI and Swagger UIHow the springdoc-openapi library describes the blog API in the OpenAPI format by itself and shows a Swagger UI page where you can send requests right from the browser.
4.Database
- Connecting a databaseWhat JPA, Hibernate and Spring Data are, how to connect an H2 database for development and what to change for PostgreSQL.
- EntitiesHow to describe a database table with a Java class and the @Entity, @Id and @Column annotations.
- RepositoriesHow Spring Data JPA builds a repository from a single interface and how to move the blog service to the database.
- Queries from method namesHow Spring Data builds an SQL query from a repository method name alone: findBy, OrderBy, Containing and other keywords.
- Your own queries: @QueryHow to write a JPQL or plain SQL query in the @Query annotation when a method name is no longer enough.
- Relationships between entitiesHow to link posts to users and comments with @ManyToOne and @OneToMany and serve comments at a nested address.
- Pages and sortingHow to return the post list in parts with Pageable, control sorting from the address and return stable JSON with PagedModel.
- TransactionsHow @Transactional groups several database actions into one unit, rolls them back on error and saves entity changes without save.
- Database migrations: FlywayHow to manage the database schema with Flyway SQL migration files instead of ddl-auto and what to do when you need a new column.
5.Thymeleaf pages
- Thymeleaf templatesHow to serve HTML pages from Spring Boot: @Controller, the model and the Thymeleaf attributes th:text, th:each, th:href.
- FormsHow to build a new-post HTML form with Thymeleaf: th:object and th:field, validation with @Valid and BindingResult, redirect after submit.
- A shared layout and static filesHow to move the header and footer into Thymeleaf fragments, pass a parameter to a fragment and include CSS from the static folder.
6.Security
- Spring Security: a first lookWhat happens when Spring Security is added to a project: everything is locked, a temporary password in the log, 401 responses and logging in with curl.
- Users and passwordsHow to keep blog users in the database, hash passwords with PasswordEncoder and plug them into Spring Security through UserDetailsService.
- API access rulesHow to use SecurityFilterChain to open reading to everyone, close changes to guests, allow deleting only to admins and find out the current user.
- Site login: a form and a sessionHow to build your own login form for the blog pages, a cookie session, logout and showing the user name in a template.
- API login with a JWT tokenHow to give an API client a JWT token for a username and password and check it on every request with OAuth2 Resource Server and your own secret key.
7.Testing
- Tests with @SpringBootTestHow to start the whole Spring application in a test, check a service against a real in-memory database and roll data back after each test.
- Controller tests: MockMvcHow to test API addresses and pages without a real server: MockMvc, jsonPath, @WithMockUser and the CSRF token in tests.
- Repository tests: @DataJpaTestHow to quickly check repository queries with the @DataJpaTest slice on an embedded database without starting the web part of the application.
- Tests on a real PostgreSQL: TestcontainersHow to start a real PostgreSQL in a Docker container for a test with Testcontainers and connect the application to it with the @ServiceConnection annotation.
8.Useful features
- Calling other APIs: RestClientHow a Spring Boot application calls someone else's HTTP API with RestClient, reads JSON into a record and handles a 404.
- Scheduled tasksHow to run a method on a schedule with @EnableScheduling and @Scheduled: intervals, cron expressions and a schedule from settings.
- CachingHow to remember method results with @Cacheable, drop stale values with @CacheEvict and check that the database is no longer hit.
- LoggingHow to write messages to the log with SLF4J, read a Spring Boot log line, control levels and save the log to a file.
9.Going to production
- Actuator and health checksHow Spring Boot Actuator reports the application's state: the /actuator/health address, /actuator/info and what is safe to expose.
- Building a JAR and running it on a serverHow to package the application into one executable JAR, run it on a server with the prod profile and pass the database password through environment variables.
- A Docker imageHow to package a Spring Boot application into a Docker image with a Dockerfile or the spring-boot:build-image command and run it together with PostgreSQL.