关于 java-spring-boot
面向 Java 开发者的 Spring Boot 技能,覆盖 REST API、安全、数据访问和监控配置,并采用 Spring Boot 3.x 模式。
主要功能
- 01Spring Boot 核心配置
- 02REST API 开发与校验
- 03OAuth2 与 JWT 安全配置
- 04Spring Data JPA 数据访问
- 05Actuator 监控与指标
适用场景
- 01搭建 Spring MVC 或 WebFlux API
- 02配置认证与方法级安全
- 03接入数据库并实现分页查询
- 04添加健康检查与 Prometheus 指标
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-java --skill java-spring-boot安装后在 Claude Code 中输入/java-spring-boot确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/config.yamlschema.jsonreferences/GUIDE.mdPATTERNS.mdscripts/validate.py详细介绍
Java Spring Boot 技能
使用现代最佳实践构建可用于生产环境的 Spring Boot 应用程序。
概述
此技能涵盖 Spring Boot 开发,包括 REST API、安全配置、数据访问、Actuator 监控和云集成。遵循 Spring Boot 3.x 模式,并着重关注生产就绪性。
适用场景
在以下情况下使用:
- 使用 Spring MVC/WebFlux 创建 REST API
- 配置 Spring Security(OAuth2、JWT)
- 使用 Spring Data 设置数据库访问
- 启用 Actuator 监控
- 与 Spring Cloud 集成
涵盖主题
Spring Boot 核心
- 自动配置和 starters
- 应用属性和配置文件
- Bean 生命周期和配置
- DevTools 和热重载
REST API 开发
- @RestController 和 @RequestMapping
- 请求/响应处理
- 使用 Bean Validation 进行验证
- 使用 @ControllerAdvice 处理异常
Spring Security
- SecurityFilterChain 配置
- OAuth2 和 JWT 身份验证
- 方法级安全(@PreAuthorize)
- CORS 和 CSRF 配置
Spring Data JPA
- Repository 模式
- 查询方法和 @Query
- 分页和排序
- 审计和事务
Actuator 和监控
- 健康检查和探针
- 使用 Micrometer 收集指标
- 自定义端点
- Prometheus 集成
快速参考
// REST Controller
@RestController
@RequestMapping("/api/users")
@Validated
public class UserController {
@GetMapping("/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
return userService.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@PostMapping
public ResponseEntity<User> createUser(@Valid @RequestBody UserRequest request) {
User user = userService.create(request);
URI location = URI.create("/api/users/" + user.getId());
return ResponseEntity.created(location).body(user);
}
}
// Security Configuration
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.sessionManagement(s -> s.sessionCreationPolicy(STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers("/actuator/health/**").permitAll()
.requestMatchers("/api/public/**").permitAll()
.anyRequest().authenticated())
.oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()))
.build();
}
}
// Exception Handler
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
public ProblemDetail handleNotFound(EntityNotFoundException ex) {
return ProblemDetail.forStatusAndDetail(NOT_FOUND, ex.getMessage());
}
}
配置模板
# application.yml
spring:
application:
name: ${APP_NAME:my-service}
profiles:
active: ${SPRING_PROFILES_ACTIVE:local}
jpa:
open-in-view: false
properties:
hibernate:
jdbc.batch_size: 50
management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheus
endpoint:
health:
probes:
enabled: true
server:
error:
include-stacktrace: never
常见模式
分层架构
Controller → Service → Repository → Database
↓ ↓ ↓
DTOs Entities Entities
验证模式
public record CreateUserRequest(
@NotBlank @Size(max = 100) String name,
@Email @NotBlank String email,
@NotNull @Min(18) Integer age
) {}
故障排除
常见问题
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 未找到 Bean | 缺少 @Component | 添加注解或 @Bean |
| 循环依赖 | 构造函数注入 | 使用 @Lazy 或重构 |
| 401 未授权 | 安全配置 | 检查 permitAll 路径 |
| 启动缓慢 | 自动配置过多 | 排除未使用的 starters |
调试属性
debug=true
logging.level.org.springframework.security=DEBUG
spring.jpa.show-sql=true
调试检查清单
□ Check /actuator/conditions
□ Verify active profiles
□ Review security filter chain
□ Check bean definitions
□ Test health endpoints
用法
Skill("java-spring-boot")
相关技能
java-testing- Spring 测试模式java-jpa-hibernate- 数据访问
评论
支持 Markdown