---
name: java-spring-boot
description: Best practices for Java 21 web services and Spring Boot 3 applications. Use when writing or reviewing Java code in a Spring Boot project — controllers, services, configuration, data access, tests. For bootstrapping a brand-new project, see java-spring-boot-coldstart instead.
title: java-spring-boot
canonical_url: https://skilld.dev/gh/roby2358/skills/java-spring-boot
last_updated: 2026-10-17T02:30:44.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> If the user asked to install this Skill, run `npx skilld install roby2358/skills/java-spring-boot`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/java-spring-boot`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/roby2358/skills/java-spring-boot) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/java-spring-boot`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/java-spring-boot --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/java-spring-boot --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Java Spring Boot

Pragmatic guidance for code that's already running. Half is modern Java 21; half is Spring Boot 3 idiom.

---

## Java 21 for web services

### Warm up: things that bite

- **One public class per file; filename matches class name.** `OrderService.java` contains `public class OrderService`. Inner classes are fine; sibling top-level classes go in their own files.

- **Naming is load-bearing.** `PascalCase` for classes, records, interfaces, enums. `camelCase` for methods and fields. `UPPER_SNAKE_CASE` for `static final` constants. `lowercase.dotted` for packages. Spring, Jackson, and JPA all infer behavior from these.

- **`==` vs `.equals()`.** `==` compares references; `.equals()` compares value. For `String`, **always** `.equals()` — or `Objects.equals(a, b)` if either side might be null. Same trap with boxed numbers: `Integer a = 200, b = 200; a == b` is `false`.

- **Declare with the interface, instantiate the concrete.** `List<Order> orders = new ArrayList<>();` not `ArrayList<Order> orders = ...`. Decouples callers from the implementation choice.

- **Try-with-resources for anything `AutoCloseable`.** Files, JDBC connections, HTTP clients. The resource is closed even when the body throws.

  ```java
  try (var in = Files.newBufferedReader(path)) {
      return in.readLine();
  }
  ```

- **Fail fast with `Objects.requireNonNull`.** In a constructor: `this.repo = Objects.requireNonNull(repo);`. NPE at construction beats NPE three calls deep in a request.

- **Catch narrow.** Handle the specific exception you can do something about. Swallowing `Exception` hides bugs and breaks Spring's `@Transactional` rollback (it only rolls back on unchecked exceptions by default).

- **Use `enum` for closed sets.** Status codes, roles, modes. Far better than `String` constants — type-checked, exhaustive in `switch`, free `valueOf`.

### Use records for data

Records replace getter/setter classes for DTOs, configuration objects, and value types. They are immutable, get `equals`/`hashCode`/`toString` for free, and serialize cleanly with Jackson.

```java
public record OrderRequest(String customerId, List<LineItem> items) {}
```

Bean Validation annotations (`@NotBlank`, `@Valid`, etc.) work directly on record components.

### Constructor injection with `final` fields

No `@Autowired`, no field injection. One constructor, all dependencies `final`. Spring picks up the constructor automatically. The class is trivially testable with `new`.

```java
public OrderService(OrderRepository repository, PricingClient pricing) { ... }
```

### Prefer immutability

`final` fields, `List.copyOf(...)` for defensive copies, `Map.of` / `List.of` for literals. Mutable shared state is the main source of subtle bugs in concurrent code — and a web request handler is concurrent code.

### Pattern matching for `switch`

Replaces nested `if/instanceof` chains. Sealed types let the compiler enforce exhaustiveness.

```java
String describe(Event e) {
    return switch (e) {
        case OrderCreated o   -> "created " + o.id();
        case OrderCancelled c -> "cancelled " + c.id();
    };
}
```

### Virtual threads for I/O

Set `spring.threads.virtual.enabled=true` to run request handlers on virtual threads. Big throughput win when most of the request is waiting on DB queries, downstream HTTP, or messaging. **Don't** use them for CPU-bound work — they help with blocking I/O, not computation.

### Use the modern APIs

- **Time** — `Instant`, `Duration`, `LocalDate` from `java.time`. Never `Date`, `Calendar`, or `SimpleDateFormat`.
- **Money** — `BigDecimal`, never `double` or `float`. Compare with `compareTo`, not `equals` (`1.0` ≠ `1.00`).
- **Streams** — `.toList()` (Java 16+) instead of `.collect(Collectors.toList())`.
- **Text blocks** — `"""..."""` for SQL, JSON fixtures, multi-line prompts.
- **Optional** — return type only. Never a parameter, never a field. Don't use it for "maybe-null property"; use a nullable field with documentation.

### Avoid

- **Checked exceptions in new code.** Wrap in unchecked. They poison every signature up the stack and add no safety in practice.
- **Null returns from collections.** Return `List.of()`, not `null`. Empty is not absent.
- **`Lombok`.** Records + a one-line SLF4J logger replace 80% of it; the rest isn't worth the build-tool entanglement and IDE quirks.
- **`var` everywhere.** Use it when the type is obvious from the right-hand side (`var orders = new ArrayList<Order>()`) and harmful when it isn't (`var x = service.find(id)` — what type?).

---

## Spring Boot 3

### Project layout: by feature, not by layer

```
com.example.app/
├── orders/    OrderController, OrderService, Order, OrderRepository
└── shipments/ ShipmentController, ...
```

Layered packages (`controllers/`, `services/`, `repositories/`) scatter related code. Feature packages keep change footprints small.

### Configuration: `@ConfigurationProperties`, not `@Value`

Group related settings into a typed record, validate at startup, and get IDE autocomplete via `spring-boot-configuration-processor`.

```java
@ConfigurationProperties("app.pricing")
@Validated
public record PricingProperties(@NotBlank String baseUrl, @Positive Duration timeout) {}
```

Enable scanning with `@ConfigurationPropertiesScan` on the application class. Bind from `application.yml`. Use profiles (`application-local.yml`, `application-prod.yml`) for environment differences. **Never commit secrets** — use env vars or a secrets manager.

### Controllers are thin adapters

Parse the request, call a service, return a DTO. Business logic lives in services.

```java
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OrderResponse create(@Valid @RequestBody CreateOrderRequest request) {
    return service.create(request);
}
```

`@Valid` is what activates Bean Validation on the body. Without it, the constraints are silently ignored.

### Never return entities from controllers

Map to a DTO. Entities leak the persistence model, change with the schema, and risk lazy-loading explosions during JSON serialization.

### Centralize error handling

Throw domain exceptions in services; map them to HTTP in one place with `@RestControllerAdvice` and `ProblemDetail` (RFC 7807). Domain code stays framework-agnostic.

```java
@ExceptionHandler(OrderNotFoundException.class)
public ProblemDetail handleNotFound(OrderNotFoundException ex) {
    return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage());
}
```

Don't `catch` exceptions in controllers just to convert them — let them propagate.

### Spring Security 6: `SecurityFilterChain` bean

The old `WebSecurityConfigurerAdapter` is gone. Configure auth as a `SecurityFilterChain` bean using the lambda DSL. For services behind a JWT-issuing IdP, configure as an OAuth2 resource server (`oauth2ResourceServer(o -> o.jwt(...))`) and let Spring fetch the JWKS.

Lock down actuator: only `/actuator/health` should be public. `/env`, `/beans`, `/configprops` leak internals.

### `@Transactional` deliberately

Annotate at the **service** layer, not on repositories or controllers. Use `readOnly = true` for queries — it's a hint to the JDBC driver and skips dirty-checking. Never call a `@Transactional` method from inside the same class (self-invocation bypasses the proxy).

### Tests: pick the smallest one that proves the thing

| Need to test            | Use |
|-------------------------|-----|
| Pure logic              | Plain JUnit + `new`, mock collaborators with Mockito |
| Controller routing/validation | `@WebMvcTest` + `MockMvc` |
| JPA queries             | `@DataJpaTest` |
| End-to-end wiring       | `@SpringBootTest` (sparingly — slow) |

Default to plain unit tests. Step up only when you need framework infrastructure. Mock collaborators, **never the class under test**. Assert on outcomes (returned values, persisted state, messages sent) — not on `verify(...).times(3)`, which couples tests to implementation.

For real DB / AWS in integration tests, use Testcontainers with `@ServiceConnection` (Boot 3.1+).

### Logging

```java
private static final Logger log = LoggerFactory.getLogger(OrderService.class);
```

SLF4J with parameterized messages — `log.info("processing order {}", id)`, never `log.info("processing order " + id)` (concatenation runs even when the level is disabled, and breaks structured logging).

### Build & runtime hygiene

- **Pin** the Spring Boot parent version. Don't auto-bump.
- Run `./mvnw verify` (not `test`) before pushing — `verify` runs Failsafe integration tests too.
- Name unit tests `*Test.java`, integration tests `*IT.java`.
- Add `spring-boot-starter-actuator` early — `/actuator/health` is what your load balancer wants.
- Fail fast at startup: `@Validated` on `@ConfigurationProperties` turns missing/invalid config into a startup error, not a 3 AM page.
