java-coding-standards
OfficialSpring Boot服务的Java编码标准:命名、不可变性、Optional用法、流、异常、泛型和项目布局。
What this skill does
When applied, it prepends a system prompt before your request is sent — no extra calls and no change to how you are billed beyond the added tokens.
---
name: java-coding-standards
description: "Spring Boot服务的Java编码标准:命名、不可变性、Optional用法、流、异常、泛型和项目布局。"
origin: ECC
---
# Java 编码规范
适用于 Spring Boot 服务中可读、可维护的 Java (17+) 代码的规范。
## 何时激活
* 在 Spring Boot 项目中编写或审查 Java 代码时
* 强制执行命名、不可变性或异常处理约定时
* 使用记录类、密封类或模式匹配(Java 17+)时
* 审查 Optional、流或泛型的使用时
* 构建包和项目布局时
## 核心原则
* 清晰优于巧妙
* 默认不可变;最小化共享可变状态
* 快速失败并提供有意义的异常
* 一致的命名和包结构
## 命名
```java
// PASS: Classes/Records: PascalCase
public class MarketService {}
public record Money(BigDecimal amount, Currency currency) {}
// PASS: Methods/fields: camelCase
private final MarketRepository marketRepository;
public Market findBySlug(String slug) {}
// PASS: Constants: UPPER_SNAKE_CASE
private static final int MAX_PAGE_SIZE = 100;
```
## 不可变性
```java
// PASS: Favor records and final fields
public record MarketDto(Long id, String name, MarketStatus status) {}
public class Market {
private final Long id;
private final String name;
// getters only, no setters
}
```
## Optional 使用
```java
// PASS: Return Optional from find* methods
Optional<Market> market = marketRepository.findBySlug(slug);
// PASS: Map/flatMap instead of get()
return market
.map(MarketResponse::from)
.orElseThrow(() -> new EntityNotFoundException("Market not found"));
```
## Streams 最佳实践
```java
// PASS: Use streams for transformations, keep pipelines short
List<String> names = markets.stream()
.map(Market::name)
.filter(Objects::nonNull)
.toList();
// FAIL: Avoid complex nested streams; prefer loops for clarity
```
## 异常
* 领域错误使用非受检异常;包装技术异常时提供上下文
* 创建特定领域的异常(例如,`MarketNotFoundException`)
* 避免宽泛的 `catch (Exception ex)`,除非在中心位置重新抛出/记录
```java
throw new MarketNotFoundException(slug);
```
## 泛型和类型安全
* 避免原始类型;声明泛型参数
* 对于可复用的工具类,优先使用有界泛型
```java
public <T extends Identifiable> Map<Long, T> indexById(Collection<T> items) { ... }
```
## 项目结构 (Maven/Gradle)
```
src/main/java/com/example/app/
config/
controller/
service/
repository/
domain/
dto/
util/
src/main/resourcUse this skill
Add a "skill" field with the skill’s ID to your chat completion request. It is applied server-side before your prompt is sent — no extra calls.
{
"model": "gpt-4o-mini",
"skill": "imp-35634260-c021-44ae-ab25-22df5c58f6b9",
"messages": [{ "role": "user", "content": "…" }]
}Install the skill, enable it in your dashboard and (optionally) limit it to specific models. It then applies automatically to every matching request — with no "skill" field to send each time.
Set it up in your dashboardMore skills
Delegate coding work to Codex, Claude Code, or OpenCode as background workers; not simple edits or read-only code lookup.
Create, edit, audit, tidy, validate, or restructure AgentSkills and SKILL.md files.
Search and analyze your own session logs (older/parent conversations) using jq.
Create, view, edit, delete, search, move, or export Apple Notes via the memo CLI on macOS.
Diagnose OpenClaw Android, iOS, or macOS node pairing, QR/setup code, route, auth, and connection failures.
Set up and use 1Password CLI for sign-in, desktop integration, and reading or injecting secrets.
List, add, edit, complete, or delete Apple Reminders and reminder lists via remindctl.
Create, search, and manage Bear notes via grizzly CLI.