์ด์ฏค๋๋ฉด ํ๋ก์ ํธ ์ ์ ์ ๊ฑฐ๋ฌํ ํ ์ค ์์์ผ ๋๋ค๊ณ ์๊ฐ์ด ๋ค์๋ค.
๊ทธ๋์ ํด๋ณด๋ ค๋๋ฐ ์๊ฐ๋ณด๋ค ์ด๋ ต๋ค, ๊ฐ ํ์ผ๋ณ๋ก ์ด๋ค ์ญํ ์ ํ๋์ง ๋ช ํํ๊ฒ ์ดํดํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ธ ๊ฒ ๊ฐ๋ค.

๐ฆcom.example.myproject
โฃ ๐controller ← ์๋ ์๋
โฃ ๐dto ← ํฌ์ฅ๋ ์์ฒญ/์๋ต
โฃ ๐entity ← ์ง์ง ๋ฐ์ดํฐ(๋ฌผ๊ฑด)
โฃ ๐config ← ํ๊ฒฝ ์ค์
โ ๐repository ← ์ฐฝ๊ณ (DB์ ์ฐ๊ฒฐ)
๊ฐ ํด๋/๊ณ์ธต ์ญํ ์์ฝ
- ๐ dto: ์๋ต/์์ฒญ ์ญํ ์ ๋งก๊ณ ์์. ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๊ทธ๋ฆ๋ค.
- PostCreateRequestDto
์ฌ์ฉ์๊ฐ ๊ฒ์๊ธ์ ์๋ก ์์ฑํ ๋, ์ ๋ชฉ๊ณผ ๋ด์ฉ ๊ฐ์ ์์ฒญ ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ๊ทธ๋ฆ - PostResponseDto
์ ์ฅ๋ ๊ฒ์๊ธ์ ์กฐํํ๊ฑฐ๋ ์ฌ์ฉ์์๊ฒ ์๋ตํ ๋, ํ์ํ ์ ๋ณด๋ง ๊ณจ๋ผ์ ๋ด๋ ๊ทธ๋ฆ - PostUpdateRequestDto
์ฌ์ฉ์๊ฐ ๊ฒ์๊ธ์ ์์ ํ ๋, ์์ ๋ ์ ๋ชฉ๊ณผ ๋ด์ฉ์ ๋ด๋ ์์ฒญ ๊ทธ๋ฆ
๐ entity: DB์ 1:1๋ก ๋งคํ๋๋ ํด๋์ค- Post๋ผ๋ ์ด๋ฆ์ผ๋ก ๋ง๋ค๊ณ
์ค์ ๋ฐ์ดํฐ(์ ๋ชฉ, ๋ด์ฉ ๋ฑ)๋ฅผ ์ ์ฅํ ํ๋๋ค์ด ๋ค์ด๊ฐ
→ ์ด ํด๋์ค๊ฐ ์ค์ DB ํ ์ด๋ธ๊ณผ ์ฐ๊ฒฐ๋จ
๐ repository: ์์ฑ๋ ๊ฒ์๊ธ์ ๋จ์ํ ์ ์ฅํ๋ ๊ฒ ์๋๋ผ, CRUD(์์ฑ/์กฐํ/์์ /์ญ์ )๊ฐ ๊ฐ๋ฅํ๋๋ก ๋์์ฃผ๋ ์ธํฐํ์ด์ค- save(), findById(), delete() ๊ฐ์ ๋ฉ์๋๊ฐ ์๋์ผ๋ก ์ ๊ณต๋จ
- JPA๊ฐ ์์์ ๊ตฌํ์ฒด ๋ง๋ค์ด์ค
๐ service: ๊ฒ์๊ธ ์์ฑ, ์์ , ์ญ์ , ์กฐํ ๋ฑ์ ํต์ฌ ๋ก์ง์ ์ฒ๋ฆฌํ๋ ๊ณต๊ฐ- Controller์ Repository ์ฌ์ด์์ ๋์
- ์ฌ์ฉ์๊ฐ ์์ฒญํ ์์ ์ ์ค์ ๋ก ์ฒ๋ฆฌํ๋ ๋น์ฆ๋์ค ๋ก์ง ๋ด๋น์
๐ controller: ์ฌ์ฉ์์ ์์ฒญ์ ๊ฐ์ฅ ๋จผ์ ๋ฐ๋ ๊ณณ, URL์ ๋งคํํด์ ์ฒ๋ฆฌํ๊ณ ์๋ต์ ๋๋ ค์ค- ์ฝ๊ฒ ๋งํ๋ฉด ์์ฒญ ์ง์ ์
- REST API ๊ธฐ์ค์ผ๋ก๋ ์ฃผ๋ก JSON ํ์์ ๋ฐ์ดํฐ๋ฅผ ์๋ต์ผ๋ก ๋๋ ค์ค
์) @PostMapping("/posts") ์ด๋ฐ ๊ฒ ์ฌ๊ธฐ์ ์ฒ๋ฆฌ๋จ
- PostCreateRequestDto
1. controller
์ฌ์ฉ์ ์์ฒญ ๋ฐ๊ณ ์๋น์ค๋ก ์ ๋ฌํ๊ณ ์๋ต์ ๋๋ ค์ฃผ๋ ๋ฌธ์ง๊ธฐ
@RestController
@RequestMapping("/posts")
public class PostController {
private final PostService postService;
@PostMapping
public PostResponseDto createPost(@RequestBody PostCreateRequestDto requestDto) {
return postService.create(requestDto);
}
}
2. dto
Entity๋ ๋ฏผ๊ฐํ ์ ๋ณด๋ ํฌํจ๋๋ฏ๋ก ์ธ๋ถ์ ํ์ํ ์ ๋ณด๋ง ๋ฐ๋ก ๊ฐ์ธ์ ์ฃผ๊ณ ๋ฐ์
Entity๋ ๋น๋ฐ๋ฒํธ ๊ฐ์ ๋ฏผ๊ฐํ ์ ๋ณด๋ DB ๊ด๋ จ ์ค์ ์ด ํฌํจ๋์ด ์์ด์, ์ด๋ฅผ ๊ทธ๋๋ก ์ธ๋ถ์ ๋ ธ์ถํ๋ฉด ์ํ
๊ทธ๋์ ์ธ๋ถ ํต์ ์๋ ํ์ํ ์ ๋ณด๋ง ๋ด์ DTO๋ฅผ ๋ฐ๋ก ๋ง๋ค์ด ์ฌ์ฉํ๋ค.
DTO๋ ๋จ๋ ์ผ๋ก ์๋ฏธ๋ฅผ ๊ฐ์ง ๊ฐ๋ ์ด ์๋๋ผ ํญ์ Entity์ ํจ๊ป ๋น๊ต๋๋ฉด์ ์ฐ์ธ๋ค.
์ฆ,
Entity: ์ง์ง ๋ฐ์ดํฐ(DB์ ์ง์ ์ฐ๊ฒฐ)
DTO: ์ ์ก์ฉ ๋ฐ์ดํฐ (ํ๋ก ํธ์๋์ ์ฃผ๊ณ ๋ฐ๋ ๊ฐ๊ณต๋ ๋ฐ์ดํฐ)
@Getter
@AllArgsConstructor
public class UserResponseDto {
private String username; // password๋ ์ ์ธ
}
@Entity
public class User {
@Id
private Long id;
private String username;
private String password; // ์ ๋ ์ธ๋ถ์ ๋ณด์ฌ์ฃผ๋ฉด ์ ๋จ
}
3. entity
DBํ ์ด๋ธ๊ณผ 1:1๋งคํ๋จ
@Entity, @Id, @Colum ๊ฐ์ ์ด๋ ธํ ์ด์ ๋ถ์
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
// ์์ฑ์, getter ๋ฑ...
}
4. config
ํ๋ก์ ํธ ์ ๋ฐ์ ์ธ ์ค์ : CORS, ํํฐ, ์ธ์ฆ, ๋น ๋ฑ๋ก ๋ฑ
์๋ ์คํ๋ง์ด ์๋์ผ๋ก ์ฝ๋๋ค.
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
}
ํ๋ฆ ์ ๋ฆฌ
[์ฌ์ฉ์] → Controller → DTO → Service → Entity → Repository → DB ↓ ๊ฒฐ๊ณผ DTO ์๋ต
entity ์ dto๋ฅผ ๋ฐ๋ก ์จ์ผ ํ๋ ์ด์
๋ณด์๊ณผ ์ญํ ๋ถ๋ฆฌ
Entity๋ ๊ทธ๋๋ก ์ธ๋ถ์ ์ฃผ๋ฉด DB๊ตฌ์กฐ๊ฐ ๋ ธ์ถ๋จ
'Backend > ๐ฑ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Spring Bean ์๋ช ์ฃผ๊ธฐ (1) | 2026.01.12 |
|---|---|
| [Spring]API ๋ก๊น (0) | 2025.08.21 |
| [Spring]Filter (0) | 2025.07.29 |
| [Spring] filter์ค์ต๊น์ง ์งํํ ํ ์ดํดํ ๋ด์ฉ ์ ๋ฆฌ (1) | 2025.07.29 |
| [Spring] Bean Validation (5) | 2025.07.28 |