spring-conventions

A set of project rules for Spring Boot, a Java framework for web applications and services, including its security, dependency injection, validation, and transaction patterns.

In plain words
What is it for?
Use it when changing Spring controllers, services, configuration, exception handlers, validation, transactions, or Gradle dependencies.
Why use it?
It helps new code follow the project's established structure instead of introducing different ways to configure services, controllers, or database work.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/e-gov/cursor-prompts/spring-conventions
Clone the repo
git clone --depth 1 https://github.com/e-gov/cursor-prompts
Per session 3,560 This file is loaded in full into every session.
When invoked 3,560 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.03560 $0.03560
Opus 5 $0.01780 $0.01780
Sonnet 5 $0.00712 $0.00712
Haiku 4.5 $0.00356 $0.00356

Measured 2d ago against content hash 28fcac754117, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

spring-conventions scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules/java-spring-boot/spring-conventions.mdc · 407 lines

How it starts

The opening of the file, as written. The whole thing — 407 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Project Spring Boot & Security Conventions

Context

  • Apply when writing or modifying Spring Boot application code, especially configuration, controllers, services, and exception handlers within this project.
  • Ensures adherence to established patterns for security, dependency injection, validation, transaction management, exception handling, and configuration.
  • Note: This project uses Gradle for build automation and dependency management.
  • Note: Some requirements are Spring Boot 4+ specific (marked accordingly). This project supports both Spring Boot 3.5 and 4.0.
  • For comprehensive security best practices, see [security.mdc](mdc:rules/common/security.mdc).
  • For jOOQ DSL usage, see mdc:rules/java-common/jooq-database-access.mdc.
  • For database retry logic, see mdc:rules/java-spring-boot/spring-retry-conventions.mdc.

Requirements

  • Dependency Injection: Use constructor injection for all Spring-managed dependencies.
  • Transaction Management: Define transactions at service method level using @Transactional. Use @Transactional(readOnly = true) for read-only methods and default @Transactional for write operations.
  • Input Validation:
    • Annotate fields within request DTOs/models with Jakarta Bean Validation annotations (e.g., @NotNull, @Size, @Pattern, @Email, @Valid for nested objects).
    • Use the @Valid annotation on @RequestBody parameters in controller methods to trigger validation.
  • Security Configuration: Configure SecurityFilterChain beans using the lambda DSL within an @Configuration class annotated with @EnableWebSecurity.
  • Stateless Security: Disable CSRF and use SessionCreationPolicy.STATELESS for stateless JWT-based APIs.
  • Authorization: Define authorization rules using authorizeHttpRequests with specific requestMatchers. Use anyRequest().authenticated() as a fallback. Ensure non-public Actuator endpoints (beyond /health) are appropriately secured (e.g., require authentication or specific roles).
  • Method-Level Security: Use @PreAuthorize and @PostAuthorize for fine-grained method-level access control when needed.
  • Enable Method Security: Enable method security via @EnableMethodSecurity in a configuration class.
  • Exception Handling: Implement centralized exception handling using @ControllerAdvice. Include an @ExceptionHandler for MethodArgumentNotValidException to return 400 Bad Request for validation failures (preferably using RFC 7807 Problem details).
  • Configuration Properties: Use @ConfigurationProperties for type-safe property binding. Enable validation with @Validated and Jakarta Bean Validation annotations.
  • HTTP Clients: Prefer RestClient (Spring Boot 3.2+) for synchronous clients and WebClient for reactive. Spring Boot 4+: RestTemplate is deprecated; migrate to RestClient. Spring Boot 3.5: RestTemplate is still available but RestClient is preferred. Configure proper timeouts, error handling, and connection pooling. Use @RestClientTest/@WebFluxTest as appropriate.
  • Caching: Use Spring Cache abstraction (@Cacheable, @CacheEvict, @CachePut) for expensive operations. Configure cache managers appropriately.
  • Async Processing: Use @Async for non-blocking operations. Configure custom TaskExecutor beans for better thread management.
  • Event Publishing: Use ApplicationEventPublisher to publish domain events for loose coupling. Handle events with @EventListener methods. Consider @TransactionalEventListener for events that should only fire after successful transaction commits.
  • Logging: Use SLF4J (org.slf4j.Logger) for logging within application components.
  • API Implementation: Implement OpenAPI generated interfaces in Controllers when applicable. NEVER modify files in src/generated/ — they are overwritten on every build.
  • JWT Converters: Define custom JwtAuthenticationConverter beans when needing specific authority extraction logic (e.g., from scopes).
  • Controllers: Keep controllers lean, delegating business logic to services. Use ResponseEntity for explicit response control.

Read the full file on GitHub · 407 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 407 lines · 3,560 tokens per session scan A 28fcac754117

Subscribe to this mod's changes

spring-conventions is a cursor rule published in the GitHub repository e-gov/cursor-prompts (34 stars, last pushed 4mo ago), licensed MIT. It adds 3,560 tokens to every session, about $0.0178 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.