Controller에서 Entity를 RequestBody로 받지 않는 이유?
2024. 9. 14. 23:32ㆍ개발/Spring
@PutMapping
public GenericResponse<Void> updateBanner(@RequestBody @Valid Banner banner) {
bannerService.updateBanner(banner);
return GenericResponse.success();
}
- validation을 하는 경우 Entity에 @NotEmpty 와 같은 어노테이션이 들어가게 된다. 그런데 어떤 로직에서 이 Entity를 사용할 때 @NotEmpty 가 필요하지 않을 수도 있다.
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Entity
public class Banner {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long bannerSeq;
@NotEmpty
private String title;
}
- Presentation Layer의 검증이 Entity 영역까지 침범하였다. 컬럼 이름이 title → bannerTitle로 바뀌었다고 하자. 그럼 API 스펙이 깨지게 된다. 또는 API 스펙이 바뀌었을 때도 문제가 될 수 있다. 따라서 별도의 API 스펙을 위한 클래스(dto)가 필요하다.
'개발 > Spring' 카테고리의 다른 글
간단한 푸시 서비스 각 부분별 스레드 설정에 따른 결과 (0) | 2024.12.12 |
---|---|
OSIV(open session in view) (0) | 2024.09.22 |
Hibernate 5와 6에서 fetch join, @BatchSize 그리고 페이징 처리 (0) | 2024.09.20 |
OneToMany, ManyToOne 테스트 (0) | 2024.09.16 |
JPA 프록시에 사용하는 ByteBuddy (0) | 2024.09.14 |