| name | java-spring-arch |
| description | Architecture reference for Java + Spring Boot 4.0 projects — Controller/Service/Repository layer responsibilities, @Transactional strategy (readOnly optimization, N+1 prevention), ExpectedException usage, and Entity↔DTO conversion patterns. |
Java + Spring Boot Architecture Guide
Layer Structure
Controller
- Role: Request validation, DTO conversion, HTTP response
- Annotations:
@RestController, @RequestMapping
- Validation:
@Valid, @Validated
- Response: Use
CommonApiResponse wrapper
Service
- Role: Business logic, transaction management
- Pattern: interface + implementation
- Transaction:
- Read:
@Transactional(readOnly = true)
- Write:
@Transactional
- Dependencies: Inject Repository via constructor injection
Repository
- Role: Data access
- JPA: Extend
JpaRepository
- Avoid N+1: Fetch Join,
@EntityGraph
Transaction Strategy
Read-only Optimization
@Transactional(readOnly = true)
public List<StudentResDto> findStudents() {
return repository.findAll().stream()
.map(StudentResDto::from)
.toList();
}
N+1 Problem Resolution
repository.findAll();
entity.getRelatedEntities();
@Query("SELECT e FROM Entity e JOIN FETCH e.relatedEntities")
List<Entity> findAllWithRelated();
Exception Handling
throw new ExpectedException("학생을 찾을 수 없습니다.", HttpStatus.NOT_FOUND);
DTO Conversion Pattern
public static StudentResDto from(Student student) {
return new StudentResDto(student.getId(), student.getName());
}