Token导航 LogoToken导航

Agent Skill · 研究检索

java-spring-service(Java spring service 搜索)

用于辅助 Java 项目开发、面向对象设计、Spring 生态、Maven 或 Gradle 依赖和后端工程实践。它适合让 Agent 分析类结构、设计接口、整理服务分层、生成测试或检查常见代码坏味道。使用时需要结合项目已有架构、包结构和依赖版本,不应只按通用教程改代码;涉及数据库、事务、并发或框架配置时,应先确认运行环境和回归测试范围。

java-spring-service 是研究检索类 Agent Skill,可用宿主为 Codex、Claude、Cursor、Gemini CLI,通过GitHub 仓库获取,权限为需要联网;许可证信息还不明确,第三方扫描已通过,按安装量在研究检索类的 73,675 个 Skill 里排第 67,168。

总安装 250周安装 10GitHub Stars 1下载 81核验 2026-05-01

安装说明

本站只整理中文说明,不托管安装包,也不代用户安装。
GitHub
许可证
unknown

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:java-spring-service(Java spring service 搜索)
来源仓库:https://github.com/dawiddutoit/custom-claude
仓库路径:skills/java-spring-service
安装命令:
npx skills add https://github.com/dawiddutoit/custom-claude --skill java-spring-service
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/dawiddutoit/custom-claude --skill java-spring-service

这个 Skill 是做什么的

研究检索类 Skill 主要解决「找资料、核对事实、把来源整理清楚」的问题,通常会让助手先检索再总结。

聚焦 Spring Boot 微服务开发的最佳实践。管理服务注册、配置中心和分布式追踪集成。提供健康检查、度量收集和优雅停机实现。适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。集群环境下需考虑配置同步和故障转移策略。

java-spring-service 属于研究检索类 Skill,可作为该场景下的辅助能力补充。

来源仓库内路径:skills/java-spring-service

上手步骤

4 步
  1. 1

    确认自己的宿主工具

    公开资料标注支持 Codex、Claude、Cursor、Gemini CLI;先确认自己用的是其中之一,再按对应工具的 Skills 目录放置。

  2. 2

    拿到安装命令

    可以直接复制的命令是 npx skills add https://github.com/dawiddutoit/custom-claude --skill java-spring-…;命令里的占位符换成自己的路径或密钥再执行。

  3. 3

    安装后重启宿主

    命令执行完要完全退出宿主工具再打开,让新装的 Skill 被重新扫描到,否则列表里可能看不到。

  4. 4

    按最小权限试用

    它需要需要联网,先在测试目录或测试账号里跑,确认没问题再放到正式项目。

常见问题

6 条

java-spring-service 怎么安装?

在终端执行 npx skills add https://github.com/dawiddutoit/custom-claude --skill java-spring-service。装完完全退出宿主工具再打开,让它重新扫描本地 Skill。

java-spring-service 支持哪些 AI 工具?

公开资料标注支持 Codex、Claude、Cursor、Gemini CLI。其他工具只要能读取本地 Skill 目录,一般也能手动放进去,但要自己确认触发方式是否一致。

java-spring-service 需要什么权限?

权限级别是需要联网。该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

java-spring-service 是免费的吗?

公开资料没有标注明确许可证,收费和商用限制都无法确认。正式使用前请到来源仓库核对授权说明。

java-spring-service 用的人多吗?

公开数据是累计安装 250 次,近一周安装 10 次,下载 81 次,GitHub 1 星。这些数字来自第三方目录统计,只能当作热度参考,不代表质量或安全性。

java-spring-service 安全吗?

第三方扫描结果为通过,但这只覆盖已知的静态检查项,不能替代自己读一遍源码和安装命令。

值得选的地方和要先想清楚的地方

值得选的地方

  • 同时适配 4 种工具,换客户端不用重新找替代品。
  • 第三方扫描已通过,常见的高风险行为没有命中。

先想清楚的地方

  • 许可证没有明确标注,能不能商用、能不能再分发都不确定。
  • 第三方目录整理,版本更新和行为细节以来源仓库为准。

适不适合你

更适合

  • 已经在用 Codex,想直接扩展它能力的人。
  • 需要处理研究检索类任务、又不想每次都手写提示词的人。

不太适合

  • 需要明确授权才能用到公司项目里的场景。
  • 需要厂商级技术支持和 SLA 的生产系统。

研究检索类 Skill 对比

6 个同类
Skill总安装GitHub 星权限许可证扫描
find-skills

寻找技能 · Codex、Claude

31,199,81316,486操作浏览器MIT有提醒
self-improving-agent

自我改进 Agent · OpenClaw

16,464,0023,396接触敏感数据MIT-0通过
soultrace

灵魂追踪 · Codex、Claude

7,588,94412需要联网未明确有提醒
self-improving

自我完善 · OpenClaw

6,829,2841,046需要联网MIT-0通过
microsoft-foundry

微软代工厂 · Codex、Claude

6,614,144764调用外部服务未明确有提醒
java-spring-service

Java spring service 搜索 · 本页

2501需要联网未明确通过

按安装量取同分类前几名;数字来自第三方目录统计,只作热度参考,不代表质量结论。

SKILL.md 原文

Spring Boot Service Generator

Table of Contents

Purpose

Generates complete, production-ready Spring Boot services following industry best practices: proper layered architecture (Controller/Service/Repository), dependency injection, exception handling with @ControllerAdvice, OpenAPI/Swagger documentation, configuration management, and comprehensive logging.

When to Use

Use this skill when you need to:

  • Create new Spring Boot REST APIs
  • Generate CRUD services with JPA repositories
  • Set up layered architecture (Controller/Service/Repository)
  • Implement RESTful endpoints with proper HTTP methods
  • Add OpenAPI/Swagger documentation
  • Configure Spring Data JPA entities and repositories
  • Create DTOs and mappers for API contracts
  • Implement global exception handling with @ControllerAdvice
  • Set up dependency injection with constructor injection
  • Generate complete Spring Boot microservices
  • Add pagination and sorting to endpoints
  • Configure database migrations with Flyway
  • Bootstrap new Spring Boot projects with best practices

Quick Start

Describe the service you need and get a complete implementation:

# Create a complete CRUD service
Create a Spring Boot service for User management with CRUD operations

# Create a specific endpoint
Create a Spring Boot REST endpoint for processing orders

Instructions

Step 1: Understand Service Requirements

Identify what needs to be created:

  • Entity/domain model (JPA entity)
  • Repository layer (Spring Data)
  • Service layer (business logic)
  • Controller layer (REST API)
  • DTOs (request/response objects)
  • Exception handling
  • Configuration
  • API documentation

Step 2: Generate Domain Model (Entity)

Create JPA entity with proper annotations:

@Entity
@Table(name = "users")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 100)
    private String name;

    @Column(nullable = false, unique = true, length = 255)
    private String email;

    @Column(name = "phone_number", length = 20)
    private String phoneNumber;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private UserStatus status;

    @CreatedDate
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updatedAt;

    @Version
    private Long version;
}

Key Annotations:

  • @Entity, @Table - JPA entity mapping
  • @Id, @GeneratedValue - Primary key generation
  • @Column - Column constraints and naming
  • @Enumerated - Enum mapping
  • @CreatedDate, @LastModifiedDate - Audit fields
  • @Version - Optimistic locking
  • @Data (Lombok) - Getters, setters, toString, equals, hashCode
  • @Builder (Lombok) - Builder pattern

Step 3: Create DTOs (Data Transfer Objects)

Separate internal models from API contracts:

// Request DTO
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CreateUserRequest {
    @NotBlank(message = "Name is required")
    @Size(min = 2, max = 100, message = "Name must be between 2 and 100 characters")
    private String name;

    @NotBlank(message = "Email is required")
    @Email(message = "Email must be valid")
    private String email;

    @Pattern(regexp = "^\\+?[1-9]\\d{1,14}$",
        message = "Phone number must be in E.164 format")
    private String phoneNumber;
}

// Response DTO
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserResponse {
    private Long id;
    private String name;
    private String email;
    private String phoneNumber;
    private UserStatus status;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
}

// Mapper
@Component
public class UserMapper {
    public User toEntity(CreateUserRequest request) {
        return User.builder()
            .name(request.getName())
            .email(request.getEmail())
            .phoneNumber(request.getPhoneNumber())
            .status(UserStatus.ACTIVE)
            .build();
    }

    public UserResponse toResponse(User user) {
        return UserResponse.builder()
            .id(user.getId())
            .name(user.getName())
            .email(user.getEmail())
            .phoneNumber(user.getPhoneNumber())
            .status(user.getStatus())
            .createdAt(user.getCreatedAt())
            .updatedAt(user.getUpdatedAt())
            .build();
    }
}

Step 4: Create Repository Layer

Use Spring Data JPA for data access:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    Optional<User> findByEmail(String email);

    List<User> findByStatus(UserStatus status);

    @Query("SELECT u FROM User u WHERE u.name LIKE %:searchTerm% OR u.email LIKE %:searchTerm%")
    Page<User> searchUsers(@Param("searchTerm") String searchTerm, Pageable pageable);

    boolean existsByEmail(String email);

    @Modifying
    @Query("UPDATE User u SET u.status = :status WHERE u.id = :id")
    int updateUserStatus(@Param("id") Long id, @Param("status") UserStatus status);
}

Key Features:

  • Extends JpaRepository for CRUD operations
  • Custom query methods (findByEmail, findByStatus)
  • @Query for complex queries
  • Pagination support with Pageable
  • @Modifying for update/delete queries

Step 5: Create Service Layer

Implement business logic with transactions:

@Service
@RequiredArgsConstructor
@Slf4j
public class UserService {
    private final UserRepository userRepository;
    private final UserMapper userMapper;
    private final ApplicationEventPublisher eventPublisher;

    @Transactional(readOnly = true)
    public UserResponse getUserById(Long id) {
        log.debug("Fetching user with id: {}", id);
        User user = userRepository.findById(id)
            .orElseThrow(() -> new UserNotFoundException(id));
        return userMapper.toResponse(user);
    }

    @Transactional(readOnly = true)
    public Page<UserResponse> getAllUsers(Pageable pageable) {
        log.debug("Fetching all users: page={}, size={}",
            pageable.getPageNumber(), pageable.getPageSize());
        return userRepository.findAll(pageable)
            .map(userMapper::toResponse);
    }

    @Transactional
    public UserResponse createUser(CreateUserRequest request) {
        log.info("Creating user: email={}", request.getEmail());

        if (userRepository.existsByEmail(request.getEmail())) {
            throw new DuplicateEmailException(request.getEmail());
        }

        User user = userMapper.toEntity(request);
        User savedUser = userRepository.save(user);

        eventPublisher.publishEvent(new UserCreatedEvent(savedUser));

        log.info("User created successfully: id={}", savedUser.getId());
        return userMapper.toResponse(savedUser);
    }

    @Transactional
    public UserResponse updateUser(Long id, UpdateUserRequest request) {
        log.info("Updating user: id={}", id);

        User user = userRepository.findById(id)
            .orElseThrow(() -> new UserNotFoundException(id));

        updateUserFields(user, request);
        User updatedUser = userRepository.save(user);

        log.info("User updated successfully: id={}", id);
        return userMapper.toResponse(updatedUser);
    }

    @Transactional
    public void deleteUser(Long id) {
        log.info("Deleting user: id={}", id);

        if (!userRepository.existsById(id)) {
            throw new UserNotFoundException(id);
        }

        userRepository.deleteById(id);
        log.info("User deleted successfully: id={}", id);
    }

    private void updateUserFields(User user, UpdateUserRequest request) {
        if (request.getName() != null) {
            user.setName(request.getName());
        }
        if (request.getPhoneNumber() != null) {
            user.setPhoneNumber(request.getPhoneNumber());
        }
    }
}

Best Practices:

  • @Transactional for database operations
  • readOnly = true for read operations (performance)
  • Comprehensive logging with SLF4J
  • Event publishing for decoupled architecture
  • Proper exception handling
  • Field-by-field updates for PATCH operations

Step 6: Create Controller Layer

Expose REST API endpoints:

@RestController
@RequestMapping("/api/v1/users")
@RequiredArgsConstructor
@Validated
@Tag(name = "User Management", description = "APIs for managing users")
public class UserController {
    private final UserService userService;

    @GetMapping("/{id}")
    @Operation(summary = "Get user by ID", description = "Retrieves a user by their unique identifier")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "User found"),
        @ApiResponse(responseCode = "404", description = "User not found")
    })
    public ResponseEntity<UserResponse> getUser(
            @PathVariable @Positive Long id) {
        UserResponse user = userService.getUserById(id);
        return ResponseEntity.ok(user);
    }

    @GetMapping
    @Operation(summary = "Get all users", description = "Retrieves a paginated list of users")
    public ResponseEntity<Page<UserResponse>> getAllUsers(
            @RequestParam(defaultValue = "0") @Min(0) int page,
            @RequestParam(defaultValue = "20") @Min(1) @Max(100) int size,
            @RequestParam(defaultValue = "id") String sortBy,
            @RequestParam(defaultValue = "ASC") Sort.Direction direction) {

        Pageable pageable = PageRequest.of(page, size, Sort.by(direction, sortBy));
        Page<UserResponse> users = userService.getAllUsers(pageable);
        return ResponseEntity.ok(users);
    }

    @PostMapping
    @Operation(summary = "Create user", description = "Creates a new user")
    @ApiResponses({
        @ApiResponse(responseCode = "201", description = "User created"),
        @ApiResponse(responseCode = "400", description = "Invalid request"),
        @ApiResponse(responseCode = "409", description = "Email already exists")
    })
    public ResponseEntity<UserResponse> createUser(
            @Valid @RequestBody CreateUserRequest request) {
        UserResponse createdUser = userService.createUser(request);
        URI location = ServletUriComponentsBuilder
            .fromCurrentRequest()
            .path("/{id}")
            .buildAndExpand(createdUser.getId())
            .toUri();
        return ResponseEntity.created(location).body(createdUser);
    }

    @PutMapping("/{id}")
    @Operation(summary = "Update user", description = "Updates an existing user")
    public ResponseEntity<UserResponse> updateUser(
            @PathVariable @Positive Long id,
            @Valid @RequestBody UpdateUserRequest request) {
        UserResponse updatedUser = userService.updateUser(id, request);
        return ResponseEntity.ok(updatedUser);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "Delete user", description = "Deletes a user by ID")
    @ApiResponse(responseCode = "204", description = "User deleted")
    public ResponseEntity<Void> deleteUser(@PathVariable @Positive Long id) {
        userService.deleteUser(id);
        return ResponseEntity.noContent().build();
    }
}

Key Features:

  • @RestController for REST endpoints
  • @RequestMapping for base path and versioning
  • OpenAPI annotations (@Operation, @ApiResponse)
  • Proper HTTP status codes (200, 201, 204, 404, etc.)
  • Request validation (@Valid, @Positive, @Min, @Max)
  • Pagination and sorting support
  • Location header for created resources

Step 7: Create Global Exception Handler

Centralized exception handling:

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleUserNotFound(
            UserNotFoundException ex, WebRequest request) {
        log.error("User not found: {}", ex.getMessage());
        ErrorResponse error = ErrorResponse.builder()
            .timestamp(LocalDateTime.now())
            .status(HttpStatus.NOT_FOUND.value())
            .error("Not Found")
            .message(ex.getMessage())
            .path(getRequestPath(request))
            .build();
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
    }

    @ExceptionHandler(DuplicateEmailException.class)
    public ResponseEntity<ErrorResponse> handleDuplicateEmail(
            DuplicateEmailException ex, WebRequest request) {
        log.error("Duplicate email: {}", ex.getMessage());
        ErrorResponse error = ErrorResponse.builder()
            .timestamp(LocalDateTime.now())
            .status(HttpStatus.CONFLICT.value())
            .error("Conflict")
            .message(ex.getMessage())
            .path(getRequestPath(request))
            .build();
        return ResponseEntity.status(HttpStatus.CONFLICT).body(error);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<ValidationErrorResponse> handleValidationErrors(
            MethodArgumentNotValidException ex, WebRequest request) {
        log.error("Validation failed: {}", ex.getMessage());

        Map<String, String> errors = ex.getBindingResult()
            .getFieldErrors()
            .stream()
            .collect(Collectors.toMap(
                FieldError::getField,
                error -> error.getDefaultMessage() != null
                    ? error.getDefaultMessage()
                    : "Invalid value"
            ));

        ValidationErrorResponse response = ValidationErrorResponse.builder()
            .timestamp(LocalDateTime.now())
            .status(HttpStatus.BAD_REQUEST.value())
            .error("Validation Failed")
            .message("Request validation failed")
            .path(getRequestPath(request))
            .errors(errors)
            .build();

        return ResponseEntity.badRequest().body(response);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handleGenericException(
            Exception ex, WebRequest request) {
        log.error("Unexpected error occurred", ex);
        ErrorResponse error = ErrorResponse.builder()
            .timestamp(LocalDateTime.now())
            .status(HttpStatus.INTERNAL_SERVER_ERROR.value())
            .error("Internal Server Error")
            .message("An unexpected error occurred")
            .path(getRequestPath(request))
            .build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .body(error);
    }

    private String getRequestPath(WebRequest request) {
        return ((ServletWebRequest) request).getRequest().getRequestURI();
    }
}

@Data
@Builder
public class ErrorResponse {
    private LocalDateTime timestamp;
    private int status;
    private String error;
    private String message;
    private String path;
}

@Data
@Builder
public class ValidationErrorResponse extends ErrorResponse {
    private Map<String, String> errors;
}

Step 8: Create Custom Exceptions

Domain-specific exceptions:

public class UserNotFoundException extends RuntimeException {
    public UserNotFoundException(Long id) {
        super("User not found with ID: " + id);
    }
}

public class DuplicateEmailException extends RuntimeException {
    public DuplicateEmailException(String email) {
        super("User already exists with email: " + email);
    }
}

Step 9: Add Configuration

Application configuration files:

application.yml:

spring:
  application:
    name: user-service

  datasource:
    url: ${DATABASE_URL:jdbc:postgresql://localhost:5432/userdb}
    username: ${DATABASE_USERNAME:postgres}
    password: ${DATABASE_PASSWORD:password}
    driver-class-name: org.postgresql.Driver

  jpa:
    hibernate:
      ddl-auto: validate
    show-sql: false
    properties:
      hibernate:
        format_sql: true
        dialect: org.hibernate.dialect.PostgreSQLDialect
        jdbc:
          batch_size: 20
        order_inserts: true
        order_updates: true

  flyway:
    enabled: true
    locations: classpath:db/migration
    baseline-on-migrate: true

server:
  port: ${PORT:8080}
  error:
    include-message: always
    include-binding-errors: always

logging:
  level:
    root: INFO
    com.example.userservice: DEBUG
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

springdoc:
  api-docs:
    path: /api-docs
  swagger-ui:
    path: /swagger-ui.html
    enabled: true

Java Config:

@Configuration
@EnableJpaAuditing
public class JpaConfig {
    // JPA auditing configuration
}

@Configuration
public class OpenApiConfig {
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
            .info(new Info()
                .title("User Service API")
                .version("1.0")
                .description("API for managing users")
                .contact(new Contact()
                    .name("API Support")
                    .email("support@example.com")))
            .servers(List.of(
                new Server().url("http://localhost:8080")
                    .description("Development server")
            ));
    }
}

Step 10: Add Database Migration

Flyway migration script:

db/migration/V1__create_users_table.sql:

CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    phone_number VARCHAR(20),
    status VARCHAR(20) NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    version BIGINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_status ON users(status);

Examples

Example 1: Complete CRUD Service

Generated Structure:

src/main/java/com/example/userservice/
├── UserServiceApplication.java
├── config/
│   ├── JpaConfig.java
│   └── OpenApiConfig.java
├── controller/
│   └── UserController.java
├── dto/
│   ├── CreateUserRequest.java
│   ├── UpdateUserRequest.java
│   └── UserResponse.java
├── entity/
│   └── User.java
├── exception/
│   ├── DuplicateEmailException.java
│   ├── UserNotFoundException.java
│   └── GlobalExceptionHandler.java
├── mapper/
│   └── UserMapper.java
├── repository/
│   └── UserRepository.java
└── service/
    └── UserService.java

src/main/resources/
├── application.yml
└── db/migration/
    └── V1__create_users_table.sql

Example 2: Custom Service with Business Logic

Requirement: Order processing service with inventory validation

Generated OrderService:

@Service
@RequiredArgsConstructor
@Slf4j
public class OrderService {
    private final OrderRepository orderRepository;
    private final InventoryService inventoryService;
    private final PaymentService paymentService;
    private final NotificationService notificationService;
    private final OrderMapper orderMapper;

    @Transactional
    public OrderResponse processOrder(CreateOrderRequest request) {
        log.info("Processing new order for customer: {}",
            request.getCustomerId());

        // Validate inventory
        validateInventoryAvailability(request.getItems());

        // Create order
        Order order = orderMapper.toEntity(request);
        order.setStatus(OrderStatus.PENDING);

        // Calculate total
        BigDecimal total = calculateOrderTotal(order);
        order.setTotal(total);

        // Save order
        Order savedOrder = orderRepository.save(order);

        // Process payment
        try {
            paymentService.processPayment(
                savedOrder.getId(),
                total,
                request.getPaymentDetails()
            );
            savedOrder.setStatus(OrderStatus.CONFIRMED);
        } catch (PaymentException e) {
            log.error("Payment failed for order: {}",
                savedOrder.getId(), e);
            savedOrder.setStatus(OrderStatus.PAYMENT_FAILED);
            throw e;
        }

        // Update inventory
        inventoryService.reserveItems(request.getItems());

        // Send confirmation
        notificationService.sendOrderConfirmation(savedOrder);

        log.info("Order processed successfully: orderId={}",
            savedOrder.getId());
        return orderMapper.toResponse(savedOrder);
    }

    private void validateInventoryAvailability(
            List<OrderItemRequest> items) {
        items.forEach(item -> {
            if (!inventoryService.isAvailable(
                    item.getProductId(),
                    item.getQuantity())) {
                throw new InsufficientInventoryException(
                    item.getProductId());
            }
        });
    }

    private BigDecimal calculateOrderTotal(Order order) {
        return order.getItems().stream()
            .map(item -> item.getPrice()
                .multiply(BigDecimal.valueOf(item.getQuantity())))
            .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}

Requirements

Dependencies (Maven pom.xml)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version>
</parent>

<dependencies>
    <!-- Spring Boot Starters -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <!-- Database -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
    </dependency>

    <!-- OpenAPI/Swagger -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.3.0</version>
    </dependency>

    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Testing -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Running the Service

# Build
mvn clean package

# Run
java -jar target/user-service-1.0.0.jar

# Or with Maven
mvn spring-boot:run

# Access Swagger UI
open http://localhost:8080/swagger-ui.html

# Access API docs
curl http://localhost:8080/api-docs

Best Practices Checklist

Architecture:

  • Clear layer separation (Controller/Service/Repository)
  • DTOs for API contracts (no entities in controllers)
  • Dependency injection via constructor
  • Single responsibility per class

API Design:

  • RESTful endpoints with proper HTTP methods
  • Correct HTTP status codes (200, 201, 204, 400, 404, etc.)
  • API versioning (/api/v1/)
  • Pagination for list endpoints
  • Location header for POST requests

Data Access:

  • @Transactional on service methods
  • readOnly = true for read operations
  • Proper exception handling (no empty catches)
  • Database migrations with Flyway/Liquibase

Validation:

  • Bean validation annotations (@NotNull, @Size, etc.)
  • @Valid on request parameters
  • Custom validators for complex rules
  • Meaningful validation error messages

Documentation:

  • OpenAPI/Swagger annotations
  • API descriptions and examples
  • README with setup instructions
  • Code comments for complex logic

Error Handling:

  • @RestControllerAdvice for global handling
  • Custom exceptions for domain errors
  • Consistent error response format
  • Proper logging of errors

Logging:

  • SLF4J with appropriate levels
  • Structured logging (JSON in production)
  • Log method entry/exit for important operations
  • Log with context (IDs, user info)

Security:

  • Input validation and sanitization
  • No sensitive data in logs
  • HTTPS in production
  • Authentication/authorization if needed

Output Format

When generating a Spring Boot service, provide:

  1. Project structure with all files
  2. Complete code for each layer
  3. Configuration files (application.yml, pom.xml)
  4. Database migrations (Flyway scripts)
  5. Setup instructions for running the service
  6. API documentation with example curl commands

Example output:

## Generated Spring Boot Service: User Management

**Structure Created:**
- Entity: User
- Repository: UserRepository
- Service: UserService
- Controller: UserController
- DTOs: CreateUserRequest, UpdateUserRequest, UserResponse
- Exceptions: UserNotFoundException, DuplicateEmailException
- Global Exception Handler
- Configuration (JPA, OpenAPI)
- Database migration

**Endpoints:**
- GET /api/v1/users - List all users
- GET /api/v1/users/{id} - Get user by ID
- POST /api/v1/users - Create user
- PUT /api/v1/users/{id} - Update user
- DELETE /api/v1/users/{id} - Delete user

**Setup:**
1. Configure database in application.yml
2. Run: mvn spring-boot:run
3. Access Swagger UI: http://localhost:8080/swagger-ui.html

**Example Request:**

curl -X POST http://localhost:8080/api/v1/users \ -H "Content-Type: application/json" \ -d '{"name":"John Doe","email":"john@example.com"}'

Error Handling

If service generation cannot be completed:

  1. Missing requirements: Ask for entity details, fields, relationships
  2. Ambiguous business logic: Request clarification on rules and validations
  3. Database type unclear: Ask which database (PostgreSQL, MySQL, etc.)
  4. Spring version conflict: Verify Spring Boot version requirements

Always provide complete, working code that follows Spring Boot best practices.

平台分布

Codex

34.86%
按下载量换算28

Claude

29.89%
按下载量换算24

Cursor

16.41%
按下载量换算13

Gemini CLI

9.04%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。目前只有一个公开来源,建议同时核对源仓库。

继续浏览同类 Skills