FrameWork/Spring
등록/수정/조회 API
태윤2
2021. 8. 19. 23:47
API를 만들기 위해 필요한 클래스
- Request 데이터를 받을 Dto(Data Transfer Object)
- API 요청을 받을 Controller
- 트랜잭션, 도메인 기능 간의 순서를 보장하는 Service
- Service에서 비지니스 로직을 처리하는게 아닌 트랜잭션, 도메인 간 순서 보장의 역할만 함
- Web Layer
- 흔히 사용하는 컨트롤러(@Controller)와 JSP/Freemarker 등의 뷰 템플릿 영역
- 이외에도 필터(Filter), 인터셉터, 컨트롤러 어드바이스(@ControllerAdvice) 등 외부 요청과 응답에 대한 전반적인 영역을 이야기
- Service Layer
- @Service에 사용되는 서비스 영역
- 일반적으로 Controller와 Dao(Data Access Object)의 중간 영역에서 사용
- @Transactional이 사용되어야 하는 영역
- Repository Layer
- Database와 같이 데이터 저장소에 접근하는 영역
- DAO 영역으로 이해하면 쉬움
- Dtos
- Dto는 계층 간에 데이터 교환을 위한 객체를 이야기하며 Dtos는 이들의 영역을 얘기
- 예를 들어 뷰 템플릿 엔진에서 사용될 객체나 Repository Layer에서 결과로 넘겨준 객체 등
- Domain Model
- 도메인이라 불리는 개발 대상을 모든 사람이 동일한 관점에서 이해할 수 있고 공유할 수 있도록 단순화시킨 것을 도메인 모델이라고 함
- 이를타면 택시 앱이라고하면 배차, 탑승,요금 등이 모두 도메인이 될 수 있음
- @Entity를 사용된 영역 역시 도메인 모델이라고 이해 하면됨
- 다만, 무조건 데이터베이스의 테이블과 관계가 있어야만 하는 것은 아님
- VO(Value Object)처럼 값 객체들도 이 영역에 해당하기 때문
- Web(Controller), Service, Repository, Dto, Domain 에서 비지니스 처리를 담당하는 곳은 Domain
Controller
@RequiredArgsConstructor
@RestController
public class PostsApiController {
private final PostsService postsService;
@PostMapping("/api/v1/posts")
public Long save(@RequestBody PostsSaveRequestDto requestDto) {
return postsService.save(requestDto);
}
@PutMapping("/api/v1/posts/{id}")
public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto) {
return postsService.update(id, requestDto);
}
@GetMapping("/api/v1/posts/{id}")
public PostsResponseDto findById(@PathVariable Long id) {
return postsService.findById(id);
}
}
- @PathVariable
- endpoint에 {}안에 값을 매핑 시켜주는 annotation
Service
@RequiredArgsConstructor
@Service
public class PostsService {
private final PostsRepository postsRepository;
@Transactional
public Long save(PostsSaveRequestDto requestDto) {
return postsRepository.save(requestDto.toEntity()).getId();
}
@Transactional
public Long update(Long id, PostsUpdateRequestDto requestDto) {
Posts posts = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. ID = " + id));
posts.update(requestDto.getTitle(), requestDto.getContent());
return id;
}
public PostsResponseDto findById(Long id) {
Posts entity = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + id));
return new PostsResponseDto(entity);
}
}
- DI
- @Autowired
- setter
- 생성자( 가장 권장하는 방식)
- @RequiredArgsConstructor(lombok)을 통해 final이 선언된 모든 필드를 인자값으로 하는 생성자 생성
- 의존성 관계가 변경될 때마다 생성자 코드를 계속해서 수정하는 번거로움 해결 ( 생산성 증가)
Dto
@Getter
@NoArgsConstructor
public class PostsSaveRequestDto {
private String title;
private String content;
private String author;
@Builder
public PostsSaveRequestDto(String title, String content, String author) {
this.title = title;
this.content = content;
this.author = author;
}
public Posts toEntity() {
return Posts.builder()
.title(title)
.content(content)
.author(author)
.build();
}
}
---------------------------------------------------------------------------------
@Getter
@NoArgsConstructor
public class PostsUpdateRequestDto {
private String title;
private String content;
@Builder
public PostsUpdateRequestDto(String title, String content) {
this.title = title;
this.content = content;
}
}
---------------------------------------------------------------------------------
@Getter
public class PostsResponseDto {
private Long id;
private String title;
private String content;
private String author;
public PostsResponseDto(Posts entity) {
this.id = entity.getId();
this.title = entity.getTitle();
this.content = entity.getContent();
this.author = entity.getAuthor();
}
}
- Entity 클래스와 거의 유사한 형태인 Dto 클래스를 추가로 생성
- 절대로 Entity 클래스를 Request/Response 클래스로 사용해선 안된다!
- Entity 클래스는 데이터베이스와 맞닿은 핵심 클래스 단순한 화면 변경을위해서 Entity클래스 변경을 하는것은 너무 큰 변경임
- Entity 클래스가 변경되면 많은 클래스에 영향을 끼치지만, Request와 Response용 Dto는 View를 위한 클래스라 자주 변경이 필요함
- View Layer와 DB Layer의 역할 분리를 철저하게 하는게 좋음
- Controller에서 결괏값으로 여러 테이블을 조인해서 줘야할 경우가 빈번 하므로 Entity 클래스만으로 표현하기 어려운 경우가 많음
- Entity 클래스와 Controller에서 쓸 Dto는 꼭 분리해서 사용하자
- update 기능에서 데이터베이스에 쿼리를 날리는 부분이 없는데 그이유는 JPA의 영속성 컨텍스트 때문
- 영속성 컨텍스트
- 엔티티를 영구 저장하는 환경(논리적 개념)
- JPA의 핵심 내용은 엔티티가 영속성 컨텍스트에 포함되어 있냐 아니냐로 갈림
- JPA의 EntityManager가 활성된 상태로 트랜잭션 안에서 데이터베이스에서 데이터를 가져오면 이 데이터는 영속성 컨텍스트가 유지된 상태
- 이상태에서 해당데이터를 변경하면(더티체킹, 변경감지)하게되면 트랜잭션이 끝나는 시점에 해당 테이블에 변경분을 반영
Test(JUnit5)
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PostsApiControllerTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private PostsRepository postsRepository;
@AfterEach
public void tearDown() throws Exception {
postsRepository.deleteAll();
}
@Test
public void postsCreate() throws Exception {
//given
String title = "title";
String content = "content";
PostsSaveRequestDto requestDto = PostsSaveRequestDto.builder()
.title(title)
.content(content)
.author("author")
.build();
String url = "http://localhost:" + port + "/api/v1/posts";
//when
ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url, requestDto, Long.class);
//then
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody()).isGreaterThan(0L);
List<Posts> all = postsRepository.findAll();
assertThat(all.get(0).getTitle()).isEqualTo(title);
assertThat(all.get(0).getContent()).isEqualTo(content);
}
@Test
public void postsModify() throws Exception {
//given
Posts savedPosts = postsRepository.save(Posts.builder()
.title("title")
.content("content")
.author("author")
.build());
Long updateId = savedPosts.getId();
String expectedTitle = "title2";
String expectedContent = "content2";
PostsUpdateRequestDto requestDto = PostsUpdateRequestDto.builder()
.title(expectedTitle)
.content(expectedContent)
.build();
String url = "http://localhost:" + port + "/api/v1/posts/" + updateId;
HttpEntity<PostsUpdateRequestDto> requestEntity = new HttpEntity<>(requestDto);
System.out.println(url);
//when
ResponseEntity<Long> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Long.class);
//then
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody()).isGreaterThan(0L);
List<Posts> all = postsRepository.findAll();
assertThat(all.get(0).getTitle()).isEqualTo(expectedTitle);
assertThat(all.get(0).getContent()).isEqualTo(expectedContent);
}
}
- @WebMvcTest는 JPA기능이 작동하지 않음
- Controller와 ControllerAdvice 등 외부 연동과 관련된 부분만 활성화 됨
- JPA기능까지 한번에 테스트할 때는 @SpringBootTest와 TestRestTemplate를 사용
- 참고자료
스프링 부트와 AWS로 혼자 구현하는 웹 서비스