Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

blog

[SpringBoot/AWS EC2] MongoDB - 2 등록 본문

Web/SpringBoot

[SpringBoot/AWS EC2] MongoDB - 2 등록

hjkongkong 2021. 12. 9. 22:50

https://hjkongkong.tistory.com/11

 

[SpringBoot/AWS EC2] MongoDB - 1 (Auto-Generated Field)

본격적으로 개발을 앞서 오늘 사용 할 환경정보이다. JAVA 1.8 springBoot 2.1.9.RELEASE gradle 7.1 spring data jpa 2.4.5 // 주석처리 mongodb version v3.6.3 딱히 명시를 안한 의존성들은 springboot 2.1.9의..

hjkongkong.tistory.com

이어서 데이터베이스 연동 및 등록/수정/조회 API를 만들 것이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.example.springboot.web.dto;
 
import com.example.springboot.domain.posts.Posts;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
 
@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();
    }
}
 
cs

PostsSaveRequestDto.java

Posts Class와 굉장히 유사함에도 PostsSaveRequestDto을 만든 이유는 Entity Class 역할을 하는 Posts Class를 Request/Response 클래스로 사용하지 않기 위함이다.

Entity Class를 기준으로 데이터베이스나 컬랙션이 생성되기때문이다. 

많은 서비스 클래스나 비지니스 로직들이 Entity Class를 기준으로 생성되어있기때문에, Entity Class는 쉽게 변경되면 안된다. Entity Class와 Controller에 쓸 Dto는 분리해서 사용하는 것이 좋다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.springboot.service;
 
import com.example.springboot.domain.posts.PostsRepository;
import com.example.springboot.web.dto.PostsSaveRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@RequiredArgsConstructor
@Service
public class PostsService {
    private final PostsRepository postsRepository;
 
    @Transactional
    public Long Save(PostsSaveRequestDto requestDto){
        return postsRepository.save(requestDto.toEntity()).getId();
    }
}
 
cs

PostsService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.springboot.web;
 
import com.example.springboot.service.PostsService;
import com.example.springboot.web.dto.PostsSaveRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
@RequiredArgsConstructor
@RestController
public class PostsApiController {
 
    private final PostsService postsService;
 
    @PostMapping("/api/v1/posts")
    public Long Save(@RequestBody PostsSaveRequestDto requestDto){
        return postsService.Save(requestDto);
    }
}
 
cs

PostsApiController.java

Service와 Controller 사이에 Autowired(의존 객체의 타입에 해당하는 Bean을 자동으로 매핑)가 없는 이유

@RequiredArgsConstructor은 final이 선언된 모든 필드를 인자값으로 하는 생성자를 생성해준다.(롬복)

클래스의 의존성 관계가 변경되더라도 생성자 코드를 수정 할 필요가 없어진다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example.springboot.web;
 
import com.example.springboot.domain.posts.Posts;
import com.example.springboot.domain.posts.PostsRepository;
import com.example.springboot.web.dto.PostsSaveRequestDto;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.util.List;
 
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PostsApiControllerTest {
 
    @LocalServerPort
    private int port;
 
    @Autowired
    private TestRestTemplate restTemplate;
 
    @Autowired
    private PostsRepository postsRepository;
 
    @After
    public void tearDown() throws Exception{
        postsRepository.deleteAll();
    }
 
    @Test
    public void Posts_등록된다() 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);
    }
}
 
cs

PostsApiControllerTest.java