Loading...
Loading...
Java and Spring Boot expert including REST APIs, JPA, and microservices
npx skill4agent add oimiragieo/agent-studio java-expertExecutors.newVirtualThreadPerTaskExecutor()// Enable virtual threads in Spring Boot 3.2+
// application.properties
spring.threads.virtual.enabled=true
// Or programmatically
@Bean
public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
return protocolHandler -> {
protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
};
}
// Using virtual threads directly
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
executor.submit(() -> {
// I/O-bound task
Thread.sleep(1000);
return "result";
});
}// Pattern matching for switch
String result = switch (obj) {
case String s -> "String: " + s;
case Integer i -> "Integer: " + i;
case Long l -> "Long: " + l;
case null -> "null";
default -> "Unknown";
};
// Record patterns
record Point(int x, int y) {}
if (obj instanceof Point(int x, int y)) {
System.out.println("x: " + x + ", y: " + y);
}public record UserDTO(String name, String email, LocalDate birthDate) {
// Compact constructor for validation
public UserDTO {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
}public sealed interface Result<T> permits Success, Failure {
record Success<T>(T value) implements Result<T> {}
record Failure<T>(String error) implements Result<T> {}
}src/main/java/com/example/app/
├── controller/ # REST endpoints (RestController)
├── service/ # Business logic (Service)
│ └── impl/ # Service implementations
├── repository/ # Data access (Repository)
├── model/
│ ├── entity/ # JPA entities
│ └── dto/ # Data Transfer Objects
├── config/ # Configuration classes
├── exception/ # Custom exceptions and handlers
└── util/ # Utility classes@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
@GetMapping("/{id}")
public ResponseEntity<UserDTO> getUser(@PathVariable Long id) {
UserDTO user = userService.findById(id);
return ResponseEntity.ok(user);
}
@PostMapping
public ResponseEntity<UserDTO> createUser(@Valid @RequestBody CreateUserDTO dto) {
UserDTO created = userService.create(dto);
return ResponseEntity.status(HttpStatus.CREATED).body(created);
}
}@Service@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final ModelMapper modelMapper;
@Override
public UserDTO findById(Long id) {
User user = userRepository.findById(id)
.orElseThrow(() -> new UserNotFoundException(id));
return modelMapper.map(user, UserDTO.class);
}
@Override
@Transactional
public UserDTO create(CreateUserDTO dto) {
User user = modelMapper.map(dto, User.class);
User saved = userRepository.save(user);
return modelMapper.map(saved, UserDTO.class);
}
}JpaRepository<Entity, ID>@Query@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
@Query("SELECT u FROM User u WHERE u.createdAt > :date")
List<User> findRecentUsers(@Param("date") LocalDateTime date);
// Projection for performance
@Query("SELECT new com.example.dto.UserSummaryDTO(u.id, u.name, u.email) FROM User u")
List<UserSummaryDTO> findAllSummaries();
}@Entity@Table@Id@Columnequals()hashCode()@Entity
@Table(name = "users")
@Getter @Setter
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String name;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Order> orders = new ArrayList<>();
@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}@EntityGraphJOIN FETCH@Query("SELECT u FROM User u JOIN FETCH u.orders WHERE u.id = :id")
Optional<User> findByIdWithOrders(@Param("id") Long id);
// Pagination
Page<User> findAll(Pageable pageable);@ExtendWith(MockitoExtension.class)
class UserServiceImplTest {
@Mock
private UserRepository userRepository;
@Mock
private ModelMapper modelMapper;
@InjectMocks
private UserServiceImpl userService;
@Test
void findById_WhenUserExists_ReturnsUserDTO() {
// Given
Long userId = 1L;
User user = new User();
user.setId(userId);
UserDTO expectedDTO = new UserDTO();
when(userRepository.findById(userId)).thenReturn(Optional.of(user));
when(modelMapper.map(user, UserDTO.class)).thenReturn(expectedDTO);
// When
UserDTO result = userService.findById(userId);
// Then
assertNotNull(result);
verify(userRepository).findById(userId);
verify(modelMapper).map(user, UserDTO.class);
}
}@SpringBootTest
@AutoConfigureMockMvc
@Transactional
class UserControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Test
void createUser_WithValidData_ReturnsCreated() throws Exception {
CreateUserDTO dto = new CreateUserDTO("John Doe", "john@example.com");
mockMvc.perform(post("/api/users")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(dto)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name").value("John Doe"));
}
}<properties>
<java.version>21</java.version>
<spring-boot.version>3.2.0</spring-boot.version>
</properties>
<dependencies>
<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>
</dependencies>plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.4'
}
java {
sourceCompatibility = JavaVersion.VERSION_21
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<ErrorResponse> handleUserNotFound(UserNotFoundException ex) {
ErrorResponse error = new ErrorResponse(
"USER_NOT_FOUND",
ex.getMessage(),
LocalDateTime.now()
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException ex) {
Map<String, String> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
.collect(Collectors.toMap(
FieldError::getField,
FieldError::getDefaultMessage
));
ErrorResponse error = new ErrorResponse(
"VALIDATION_ERROR",
"Invalid input",
errors,
LocalDateTime.now()
);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
}
}@Slf4j
@Service
public class UserServiceImpl implements UserService {
public UserDTO findById(Long id) {
log.debug("Finding user with id: {}", id);
try {
User user = userRepository.findById(id)
.orElseThrow(() -> new UserNotFoundException(id));
log.info("User found: {}", user.getEmail());
return modelMapper.map(user, UserDTO.class);
} catch (UserNotFoundException ex) {
log.error("User not found with id: {}", id, ex);
throw ex;
}
}
}management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheus
endpoint:
health:
show-details: always@AutowiredOptional.get()isPresent()orElse()orElseThrow()get()NoSuchElementException@Transactional@AsyncTaskExecutor@Async@ControllerAdviceException| Anti-Pattern | Why It Fails | Correct Approach |
|---|---|---|
Field injection with | Hidden dependencies; untestable without Spring context; null in unit tests | Constructor injection; all required dependencies declared as final fields |
| | Use |
| Spring proxy bypassed; method runs outside transaction; data integrity lost | Move transactional methods to a separate service bean; inject and call from outside |
Default | Single-thread pool queues all tasks; async calls run sequentially | Configure |
Global | Swallows specific exceptions; all errors return same generic 500 response | Map specific exception types to HTTP status codes; use |
cat .claude/context/memory/learnings.mdASSUME INTERRUPTION: Your context may reset. If it's not in memory, it didn't happen.