Cursor Rules for Java Spring Boot: 10 Rules to Copy Today
Cursor rules for Java Spring Boot: 10 copy-ready .mdc rules for injection, controllers, JPA, Flyway, tests, and guardrails, plus a prompt that fits your repo.

Cursor rules for Java Spring Boot are short instruction files in .cursor/rules/ that tell the Cursor agent how your service is built: which Spring Boot version, how you inject dependencies, where controllers and entities live, and which files it must not touch. Below are 10 rules you can paste into .mdc files today, grouped by layer, and a prompt that writes a full rule set from your own pom.xml or build.gradle.
Edit the [brackets] to match your codebase. A rule only helps when it describes what your team actually does.
How a Cursor rule file is shaped
Each .mdc file starts with frontmatter: a description, optional globs that attach the rule to matching files, and alwaysApply. The body is a short list. Keep each line one checkable instruction. Split by layer so the web rules load for controllers and the test rules load for tests.
A single .cursorrules file in the project root is the legacy format. The current approach is one .mdc file per topic in .cursor/rules/. In Cursor's rule type menu, a rule with globs is "Apply to Specific Files", a rule with only a description is "Apply Intelligently" (the agent pulls it in when the description fits the task), and alwaysApply: true is "Always Apply".
Core rules (alwaysApply)
1. Versions and imports
This service uses Java [version] and Spring Boot [version]. Use jakarta.* imports, never javax.*.
2. Constructor injection
Inject dependencies through the constructor with final fields. Never put @Autowired on a field.
3. Configuration
Bind settings with @ConfigurationProperties classes under [config package]. Do not scatter @Value annotations through services.
Web layer rules (globs on your controller package)
4. DTOs at the edge
Controllers accept and return DTOs from [dto package]. Never return a JPA entity from a controller.
5. Validation and errors
Validate request bodies with @Valid and constraint annotations. Map errors through [your exception handler class]; do not add try and catch blocks in controllers.
Persistence rules (globs on repository, entity, and migration paths)
6. Transactions
Put @Transactional on service methods only. Do not add @Transactional to repositories or controllers.
Spring Data JPA repository methods are already transactional by default, so the service layer is the one place you set transaction boundaries.
7. Migrations
Schema changes go in a new [Flyway or Liquibase] file named [pattern]. Never edit a migration that has already run, and never set ddl-auto to update.
Testing rules (globs on src/test)
8. Right test for the layer
Services: JUnit 5 with Mockito and no Spring context. Controllers: @WebMvcTest. Repositories: @DataJpaTest [with Testcontainers if you use it].
9. Naming
Name tests [your pattern, for example should_doX_when_Y]. One behavior per test.
Guardrails (alwaysApply)
10. Do not touch
Do not change [build file dependencies, prod config, generated code]. Propose new dependencies in chat. Never write secrets into code or YAML.
Generate the whole rule set from your build file
Copying rules is a start, but the better set comes from your real project. The Cursor Rules for Java Spring Boot: .mdc Rule Set Generator reads your build file, package layout, persistence choices, and test stack, then writes 4 to 6 .mdc files with frontmatter, bad and good Java snippets, a conflict check, and three test tasks to try on the agent. The example on the page builds rules for a Java 21, Spring Boot 3.3 billing service with Flyway and Testcontainers.
Tips for Spring Boot rules that the agent follows
- Only describe what the build file contains. A rule about a library you do not use confuses the agent.
- Prefer globs over alwaysApply for layer rules, so each request carries less noise.
- Test the rules with a small real task, such as adding one endpoint, and tighten any rule it ignored.
- Review rule files in pull requests like code, and update them when the team changes a convention, so the agent never learns an old pattern.
- Keep secrets and environment specific values out of rule files entirely.
Related prompts
For a stack agnostic version, see Cursor Rules .mdc From a Repo Brief. Still on Spring Boot 2.7? Plan the upgrade with the Spring Boot 2.7 to 3 Migration Planner. Find more in the Agents prompts hub, browse more prompts, or share a prompt your team uses.