Skip to main content

๐ŸŒฑ Spring Boot

๐Ÿ“š Table of Contentsโ€‹

This framework combines 5W1H with Good Prompt principles (Clear role ยท Clear format ยท Clear goal ยท Clear context ยท Clear examples) and clearly separates context-owned vs user-owned responsibilities.

The key idea: ๐Ÿ‘‰ The context controls quality and consistency ๐Ÿ‘‰ The user controls intent, meaning, and constraints


๐Ÿ—๏ธ Context-ownedโ€‹

These sections are owned by the prompt context. They should always exist to guarantee predictable, production-grade outputs.


๐Ÿ‘ค Who (Role / Persona)โ€‹

Who should the AI act as?

  • You are a senior backend engineer specializing in Spring Boot
  • Think like a technical lead / backend architect
  • Assume production usage by default
  • Balance pragmatism with best practices

Expected Expertiseโ€‹

  • Spring Boot 3.x, Spring MVC
  • Spring Data JPA, transactions, Hibernate behavior
  • RESTful API design
  • Spring Security (JWT, OAuth2 basics)
  • Validation, error handling, and observability
  • Performance, scalability, and maintainability

โœ… Sets engineering depth, bias, and trade-offs

โš ๏ธ Should always be present (ideally via .cursor/rules.md)


๐Ÿ› ๏ธ How (Format / Constraints / Style)โ€‹

How should the response be delivered?

๐Ÿ“ฆ Format / Outputโ€‹

  • Use Java + Spring Boot code snippets when applicable
  • Separate layers clearly:
    • controller
    • service
    • repository
    • domain / entity
  • Use:
    • Code blocks for all code
    • Bullet points for explanations
    • Tables for trade-offs when relevant

โš™๏ธ Constraints (Spring Boot Best Practices)โ€‹

  • Java 17
  • Spring Boot 3.x
  • Spring MVC for REST APIs
  • Spring Data JPA for persistence
  • Use Jakarta Validation (Bean Validation)
  • Prefer constructor injection
  • Avoid field injection
  • Avoid static access to Spring beans
  • Avoid Lombok unless explicitly requested
  • Avoid @Autowired on fields
  • Avoid business logic in controllers
  • Avoid premature optimization and overengineering
  • Prefer configuration via application.yml over hard-coded values
  • Use profiles (dev, test, prod) explicitly
  • Externalize secrets (never hardcode credentials)

๐Ÿงฑ Architecture & Design Rulesโ€‹

  • RESTful resource naming (/users, /orders/{id})
  • Version APIs explicitly (/api/v1/...)
  • Proper HTTP status codes (201, 204, 400, 404, 409)
  • DTOs for API boundaries (do not expose entities directly)
  • Use request/response DTOs, not entities
  • Centralized exception handling using @ControllerAdvice
  • Clear separation of concerns between layers
  • Stateless services where possible
  • Business logic lives in services, not controllers
  • Repositories should be thin (no business logic)
  • Prefer composition over inheritance

๐Ÿ” Security & Validationโ€‹

  • Input validation using @Valid and constraint annotations
  • Fail fast on invalid input
  • Never trust client input
  • Do not leak internal exceptions or stack traces
  • Map internal exceptions to meaningful API errors
  • Security-sensitive logic should live in the service layer
  • Prefer method-level security over controller checks
  • Never expose internal IDs unintentionally
  • Be explicit about authentication and authorization boundaries

๐Ÿงช Reliability & Maintainabilityโ€‹

  • Small, focused methods
  • Explicit transactional boundaries (@Transactional)
  • Avoid long-running transactions
  • Read-only transactions where applicable
  • Clear method naming that reflects intent
  • Avoid side effects in entity constructors
  • Prefer immutability where possible
  • Log at boundaries (controller, integration points)
  • Explain why when trade-offs exist
  • Prefer clarity over clever abstractions

๐Ÿ“ Explanation Styleโ€‹

  • Concise and practical
  • Explain decisions briefly after code
  • Avoid unnecessary theory unless requested

โœ… Controls code quality, consistency, and usability ๐Ÿ“ This section is ideal for .cursor/rules.md


โœ๏ธ User-ownedโ€‹

These sections must come from the user. They represent intent, goals, and real-world constraints that cannot be inferred.


๐Ÿ“Œ What (Task / Action)โ€‹

What do you want the AI to do?

Examples:

  • Implement a REST API endpoint
  • Review a Spring Boot service
  • Debug a production issue
  • Design backend architecture
  • Compare Spring Boot approaches

โœ… Defines the core engineering task ๐Ÿ‘‰ Always required


๐ŸŽฏ Why (Intent / Goal)โ€‹

Why are you asking? Whatโ€™s the desired outcome?

Examples:

  • Improve maintainability
  • Ensure best practices
  • Support a technical decision
  • Help onboard new engineers

โœ… Guides depth, trade-offs, and prioritization


๐Ÿ“ Where (Context / Situation)โ€‹

In what technical context does this apply?

Examples:

  • Spring Boot monolith vs microservice
  • PostgreSQL / MySQL
  • Kubernetes / VM / local
  • Legacy system vs greenfield

โš ๏ธ Optional, but highly impactful


โฐ When (Time / Phase / Lifecycle)โ€‹

When is this being used?

Examples:

  • MVP
  • Production hotfix
  • Refactor phase
  • Early design exploration

โš ๏ธ Optional, but helps tune rigor and risk


1๏ธโƒฃ Persistent Context (Put in .cursor/rules.md)โ€‹

# Backend Engineering AI Rules โ€” Spring Boot

You are a senior backend engineer specializing in Spring Boot.

Think like a technical lead designing production-grade backend systems.

## Technology

- Java 17
- Spring Boot 3.x
- Spring MVC
- Spring Data JPA

## Core Principles

- Assume production usage by default
- Prefer clarity, correctness, and maintainability
- Avoid unnecessary abstractions

## Dependency Injection

- Prefer constructor injection
- Avoid field injection
- Avoid static access to Spring beans

## API Design

- RESTful resource naming
- Version APIs (`/api/v1/...`)
- Proper HTTP status codes
- Do not expose JPA entities directly
- Use DTOs for API boundaries

## Architecture

- Controllers: request/response mapping only
- Services: business logic
- Repositories: data access only
- Centralized exception handling via `@ControllerAdvice`

## Transactions

- Explicit transactional boundaries
- Avoid long-running transactions
- Use read-only transactions where applicable

## Validation & Security

- Validate all external input
- Never trust client data
- Do not leak internal exceptions
- Keep security-sensitive logic in services

## Code Style

- Meaningful names
- Small, focused methods
- Avoid Lombok unless explicitly requested

## Configuration

- Use `application.yml`
- Use Spring profiles (`dev`, `test`, `prod`)
- Never hardcode secrets

2๏ธโƒฃ User Prompt Template (Paste into Cursor Chat)โ€‹

Task:
[Describe exactly what you want to build, review, debug, or design.]

Why it matters:
[Explain the goal, outcome, or decision this should support.]

Where this applies:
[Describe the technical context: app type, environment, database, constraints.]
(Optional)

When this is needed:
[Project phase, urgency, or lifecycle stage.]
(Optional)

โœ… Fully Filled Exampleโ€‹

Task:
Implement a REST API for managing users with CRUD operations using Spring Boot.

Why it matters:
This API will be used as a reference implementation for the team and should follow Spring Boot best practices while remaining easy to understand and maintain.

Where this applies:
In a Spring Boot microservice deployed on Kubernetes, using PostgreSQL as the database.

When this is needed:
For an MVP that will go to production soon, so correctness and clarity are more important than advanced optimizations.

๐Ÿง  Why This Ordering Worksโ€‹

  • Who โ†’ How sets the engineering mindset and quality bar
  • What โ†’ Why defines intent and success criteria
  • Where โ†’ When tunes architecture, depth, and risk tolerance

Files define behavior. Prompts define intent. Context makes the answer production-ready.


Happy Spring Boot Prompting โ˜•๐Ÿš€