$npx -y skills add decebals/claude-code-java --skill spring-boot-patternsSpring Boot best practices and patterns. Use when creating controllers, services, repositories, or when user asks about Spring Boot architecture, REST APIs, exception handling, or JPA patterns.
| 1 | # Spring Boot Patterns Skill |
| 2 | |
| 3 | Best practices and patterns for Spring Boot applications. |
| 4 | |
| 5 | ## When to Use |
| 6 | - User says "create controller" / "add service" / "Spring Boot help" |
| 7 | - Reviewing Spring Boot code |
| 8 | - Setting up new Spring Boot project structure |
| 9 | |
| 10 | ## Project Structure |
| 11 | |
| 12 | ``` |
| 13 | src/main/java/com/example/myapp/ |
| 14 | ├── MyAppApplication.java # @SpringBootApplication |
| 15 | ├── config/ # Configuration classes |
| 16 | │ ├── SecurityConfig.java |
| 17 | │ └── WebConfig.java |
| 18 | ├── controller/ # REST controllers |
| 19 | │ └── UserController.java |
| 20 | ├── service/ # Business logic |
| 21 | │ ├── UserService.java |
| 22 | │ └── impl/ |
| 23 | │ └── UserServiceImpl.java |
| 24 | ├── repository/ # Data access |
| 25 | │ └── UserRepository.java |
| 26 | ├── model/ # Entities |
| 27 | │ └── User.java |
| 28 | ├── dto/ # Data transfer objects |
| 29 | │ ├── request/ |
| 30 | │ │ └── CreateUserRequest.java |
| 31 | │ └── response/ |
| 32 | │ └── UserResponse.java |
| 33 | ├── exception/ # Custom exceptions |
| 34 | │ ├── ResourceNotFoundException.java |
| 35 | │ └── GlobalExceptionHandler.java |
| 36 | └── util/ # Utilities |
| 37 | └── DateUtils.java |
| 38 | ``` |
| 39 | |
| 40 | --- |
| 41 | |
| 42 | ## Controller Patterns |
| 43 | |
| 44 | ### REST Controller Template |
| 45 | ```java |
| 46 | @RestController |
| 47 | @RequestMapping("/api/v1/users") |
| 48 | @RequiredArgsConstructor // Lombok for constructor injection |
| 49 | public class UserController { |
| 50 | |
| 51 | private final UserService userService; |
| 52 | |
| 53 | @GetMapping |
| 54 | public ResponseEntity<List<UserResponse>> getAll() { |
| 55 | return ResponseEntity.ok(userService.findAll()); |
| 56 | } |
| 57 | |
| 58 | @GetMapping("/{id}") |
| 59 | public ResponseEntity<UserResponse> getById(@PathVariable Long id) { |
| 60 | return ResponseEntity.ok(userService.findById(id)); |
| 61 | } |
| 62 | |
| 63 | @PostMapping |
| 64 | public ResponseEntity<UserResponse> create( |
| 65 | @Valid @RequestBody CreateUserRequest request) { |
| 66 | UserResponse created = userService.create(request); |
| 67 | URI location = ServletUriComponentsBuilder.fromCurrentRequest() |
| 68 | .path("/{id}") |
| 69 | .buildAndExpand(created.getId()) |
| 70 | .toUri(); |
| 71 | return ResponseEntity.created(location).body(created); |
| 72 | } |
| 73 | |
| 74 | @PutMapping("/{id}") |
| 75 | public ResponseEntity<UserResponse> update( |
| 76 | @PathVariable Long id, |
| 77 | @Valid @RequestBody UpdateUserRequest request) { |
| 78 | return ResponseEntity.ok(userService.update(id, request)); |
| 79 | } |
| 80 | |
| 81 | @DeleteMapping("/{id}") |
| 82 | public ResponseEntity<Void> delete(@PathVariable Long id) { |
| 83 | userService.delete(id); |
| 84 | return ResponseEntity.noContent().build(); |
| 85 | } |
| 86 | } |
| 87 | ``` |
| 88 | |
| 89 | ### Controller Best Practices |
| 90 | |
| 91 | | Practice | Example | |
| 92 | |----------|---------| |
| 93 | | Versioned API | `/api/v1/users` | |
| 94 | | Plural nouns | `/users` not `/user` | |
| 95 | | HTTP methods | GET=read, POST=create, PUT=update, DELETE=delete | |
| 96 | | Status codes | 200=OK, 201=Created, 204=NoContent, 404=NotFound | |
| 97 | | Validation | `@Valid` on request body | |
| 98 | |
| 99 | ### ❌ Anti-patterns |
| 100 | ```java |
| 101 | // ❌ Business logic in controller |
| 102 | @PostMapping |
| 103 | public User create(@RequestBody User user) { |
| 104 | user.setCreatedAt(LocalDateTime.now()); // Logic belongs in service |
| 105 | return userRepository.save(user); // Direct repo access |
| 106 | } |
| 107 | |
| 108 | // ❌ Returning entity directly (exposes internals) |
| 109 | @GetMapping("/{id}") |
| 110 | public User getById(@PathVariable Long id) { |
| 111 | return userRepository.findById(id).get(); |
| 112 | } |
| 113 | ``` |
| 114 | |
| 115 | --- |
| 116 | |
| 117 | ## Service Patterns |
| 118 | |
| 119 | ### Service Interface + Implementation |
| 120 | ```java |
| 121 | // Interface |
| 122 | public interface UserService { |
| 123 | List<UserResponse> findAll(); |
| 124 | UserResponse findById(Long id); |
| 125 | UserResponse create(CreateUserRequest request); |
| 126 | UserResponse update(Long id, UpdateUserRequest request); |
| 127 | void delete(Long id); |
| 128 | } |
| 129 | |
| 130 | // Implementation |
| 131 | @Service |
| 132 | @RequiredArgsConstructor |
| 133 | @Transactional(readOnly = true) // Default read-only |
| 134 | public class UserServiceImpl implements UserService { |
| 135 | |
| 136 | private final UserRepository userRepository; |
| 137 | private final UserMapper userMapper; |
| 138 | |
| 139 | @Override |
| 140 | public List<UserResponse> findAll() { |
| 141 | return userRepository.findAll().stream() |
| 142 | .map(userMapper::toResponse) |
| 143 | .toList(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public UserResponse findById(Long id) { |
| 148 | return userRepository.findById(id) |
| 149 | .map(userMapper::toResponse) |
| 150 | .orElseThrow(() -> new ResourceNotFoundException("User", id)); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | @Transactional // Write transaction |
| 155 | public UserResponse create(CreateUserRequest request) { |
| 156 | User user = userMapper.toEntity(request); |
| 157 | User saved = userRepository.save(user); |
| 158 | return userMapper.toResponse(saved); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | @Transactional |
| 163 | public void delete(Long id) { |
| 164 | if (!userRepository.existsById(id)) { |
| 165 | throw new ResourceNotFo |