java

A set of enterprise Java development rules for Spring Boot, Maven, Gradle, Android, and large business applications.

In plain words
What is it for?
Use it to guide naming, package structure, object-oriented design, layered architecture, repositories, services, REST APIs, and suitable design patterns.
Why use it?
It helps organize complex Java code consistently and reduces confusion between web endpoints, business logic, data access, and domain models.

Cursor rule for Cursor

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/wangqiqi/cursor-ai-rules/java
Clone the repo
git clone --depth 1 https://github.com/wangqiqi/cursor-ai-rules

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 2,917 The whole file, excluding the scripts and references it only reads on demand.
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.00000 $0.02917
Opus 5 $0.00000 $0.01458
Sonnet 5 $0.00000 $0.00583
Haiku 4.5 $0.00000 $0.00292

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

Security

Grade A, and why

java 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.

.cursor/rules/tech/java.mdc · 429 lines

How it starts

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

📜 Java 开发规则

版本: v4.3.0 | 最后更新: 2026-01-22 | 作者: Cursor AI Rules

🎯 适用场景

  • Spring Boot 微服务开发
  • 企业级 Java 应用
  • Android 应用开发
  • 大数据处理应用

🏗️ 架构原则

面向对象设计

  • SOLID 原则: 单一职责、开闭原则、里氏替换、接口隔离、依赖倒置
  • 设计模式: 在适当场景使用 GoF 设计模式
  • 组合优于继承: 优先使用组合而不是继承

分层架构

  • 表现层: Controller/REST API
  • 业务层: Service/业务逻辑
  • 数据访问层: Repository/DAO
  • 领域层: Domain Model/业务实体

📝 编码规范

命名约定

// ✅ 推荐 - 类和接口
public class UserService {
    private UserRepository userRepository;
}

public interface PaymentProcessor {
    void process(PaymentRequest request);
}

// ✅ 推荐 - 方法和变量
public UserProfile getUserProfile(String userId) {
    Optional<User> user = userRepository.findById(userId);
    return user.map(this::convertToProfile).orElse(null);
}

private UserProfile convertToProfile(User user) {
    // 转换逻辑
}

// ❌ 避免
public class userservice {  // 类名应 PascalCase
    private UserRepository userrepository;  // 变量名应 camelCase
}

public UserProfile GetUserProfile(String userid) {  // 方法名应 camelCase
    // ...
}

包结构组织

com.company.project
├── api/           # REST API 接口
├── config/        # 配置类
├── domain/        # 领域模型
│   ├── entity/    # JPA 实体
│   ├── repository/# 数据访问接口
│   └── service/   # 业务服务
├── infrastructure/# 基础设施层
│   ├── persistence/# 数据持久化
│   ├── messaging/ # 消息处理
│   └── external/  # 外部服务集成
├── application/   # 应用服务层
└── common/        # 公共组件
    ├── exception/ # 异常处理
    ├── util/      # 工具类
    └── validation/# 验证器

🔧 最佳实践

异常处理

// ✅ 推荐 - 自定义异常和统一处理
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException ex) {
        return ResponseEntity.badRequest()
                .body(new ErrorResponse(ex.getMessage()));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
        log.error("Unexpected error", ex);
        return ResponseEntity.internalServerError()
                .body(new ErrorResponse("Internal server error"));
    }
}

// 自定义业务异常
public class BusinessException extends RuntimeException {
    public BusinessException(String message) {
        super(message);
    }
}

Read the full file on GitHub · 429 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 · 429 lines · 0 tokens per session scan A 1fb59a068cdd

Subscribe to this mod's changes

java is a cursor rule published in the GitHub repository wangqiqi/cursor-ai-rules (15 stars, last pushed 3mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 2,917 tokens. 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.