๐โ๏ธ QNA ์๋น์ค ๊ธฐ๋ฅ ๊ตฌํ (๋ณต์ต) _ 78๊ฐ ~ 91๊ฐ
1๏ธโฃ QuestionController
โ Spring ์์กด์ฑ ์ฃผ์ (Dependency Injection)
๋ฐ๋ก ์ ๋ฆฌ : https://like099.tistory.com/28
(Spring) ์์กด์ฑ ์ฃผ์ ๋ฐฉ์ 3๊ฐ์ง (@Autowired, ์์ฑ์, Setter)
๐ ๊ณต๋ถํ๊ฒ ๋ ๊ณ๊ธฐ Spring ์์กด์ฑ ์ฃผ์ ์ ๋ํด์ @Autowired ์ ์์ฑ์ ์ ๋๋ง ์๊ณ ์์๋๋ฐ Setter ๋ฐฉ๋ฒ๋ ์๋ค๊ณ ํด์ ๊ฐ ๋ฐฉ๋ฒ์ ๋ํด ์ ๋ฆฌํด๋ณด๊ณ , ์ด๋ค ๋ฐฉ์์ด ํจ์จ์ ์ธ์ง๋ ํจ๊ป ์์๋ณด๊ณ ์
like099.tistory.com
โก @RequiredArgsConstructor ์ด ํด์ฃผ๋ ์ญํ (โ ์์กด์ฑ ์ฃผ์ : ์์ฑ์ ๋ฐฉ์๊ณผ ์ฐ๊ด)
// lombok ๋ฏธ์ฌ์ฉ
@Controller
public class QuestionController {
private final QuestionRepository questionRepository;
public QuestionController(QuestionRepository questionRepository) {
this.questionRepository = questionRepository;
}
}
// lombok ์ฌ์ฉ
@Controller
@RequiredArgsConstructor
public class QuestionController {
private final QuestionRepository questionRepository;
}
2๏ธโฃ QuestionService
- Service ๊ฐ ํ์ํ ์ด์
โ ๋ชจ๋ํ
์ฌ๋ฌ ์ปจํธ๋กค๋ฌ๊ฐ ๊ฐ์ ์๋น์ค๋ฅผ ํ์๋กํ ๋ ๋งค๋ฒ ๊ตฌํํ๋ ๊ฒ์ด ์๋ Service ์ ๋ชจ๋ํ ๋์ด์๋ ๊ธฐ๋ฅ์ ํธ์ถํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ด ํจ์จ์
โก ๋ณด์
Controller ๋ ์ง์ DB ์ ์ ๊ทผํ์ง ๋ชปํ๊ณ Service ๋ฅผ ํตํด์ ์ ๊ทผ ๊ฐ๋ฅํด์ผ ์ถํ ํด์ปค๊ฐ Controller ๋ฅผ ์ ์ดํด๋ ๋ฆฌํฌ์งํฐ๋ฆฌ ์ง์ ์ ๊ทผ์ ๋ง์ ์ ์๋ค.
โข Entity <-> DTO
Entity ๋ DB ์ ์ง์ ์ ์ผ๋ก ์ฐ๊ฒฐ๋์ด์์ผ๋ฏ๋ก ์ปจํธ๋กค๋ฌ์์ ์ง์ ๋ค๋ฃจ๋ ๊ฒ์ ์ข์ง ์๋ค. ๋ฐ๋ผ์ ๊ฐ ์ํฐํฐ๋ฅผ DTO ๊ฐ์ฒด๋ก ๋ณํํ์ฌ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค. ํด๋น ๊ณผ์ ์ Service ๋จ์์ ์ํํ๋ค.

DTO ์์ฑํ์ฌ ์ฌ์ฉํด๋ณด๊ธฐ (๋ชจ๋ ์ง๋ฌธ์ ๋ถ๋ฌ์ค๋ API) : https://like099.tistory.com/29
(Spring) Entity ์์ DTO ๋ณํ ํ ์ฌ์ฉ
๐ ๊ณต๋ถํ๊ฒ ๋ ๊ณ๊ธฐ ๋งค๋ฒ Entity ์ DTO ์ ์ฐจ์ด์ ์ฌ์ฉ๋ฒ์ ๋ํด ํท๊ฐ๋ฆฌ๊ณ , ์ ํ์ํ์ง์ ๋ํด ์๊ฐํ๋ ์ ์ด ๋ง๋ค. ํ๋ก์ ํธ์์๋ ๋ค๋ฅธ ํ์ ๋ถ๋ค์ DTO ๋ฅผ ์์ ๋กญ๊ฒ ์ฐ์๋๋ฐ ๊ทธ๋ ์ง ๋ชปํ ๋
like099.tistory.com
๐ ์ถ๊ฐ ๊ณต๋ถ ๋ด์ฉ
์ DTO ์์ฑํ์ฌ ์ฌ์ฉํด๋ณด๊ธฐ ๊ณต๋ถ ๊ณผ์ ์์ Entity ์ ์ฒด๋ฅผ ๋ถ๋ฌ์ค๋ ๊ฒ๊ณผ DTO ๋ฅผ ์์ฑํ์ฌ ๋ถ๋ฌ์ค๋ ๊ฒ์ ๋ฐ์ดํฐ๋ ์ฐจ์ด๋ฅผ ๋น๊ต ํ๋ ์ค ์ํ ์ฐธ์กฐ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ฌ ๊ด๋ จ ๋ด์ฉ์ ์ ๋ฆฌํด๋ณด์๋ค.
๋ฐ๋ก ์ ๋ฆฌ : https://like099.tistory.com/31
(JPA) ์๋ฑกํฅ ๊ด๊ณ์ ์ํ ์ํ ์ฐธ์กฐ ๋ฌธ์ ํด๊ฒฐ (Could not write JSON: Infinite recursion)
Spring JPA ๋ฅผ ๊ณต๋ถํ๋ ์ค ์๋ฐฉํฅ ๊ด๊ณ์ ์๋ Entity ์ค ํ๋๋ฅผ ๋ถ๋ฌ์ค๋ ๊ณผ์ ์์ ์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ฌ์ ํด๊ฒฐํ๋ ๊ณผ์ ์ ์ ๋ฆฌํด๋ณด์๋ค. Could not write JSON: Infinite recursion ... ์๋ฑกํฅ ๊ด๊ณ์
like099.tistory.com
'TIL ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| TIL 7์ผ์ฐจ (2022.10.17) (0) | 2022.10.17 |
|---|---|
| TlL 6์ผ์ฐจ (2022.10.14) (0) | 2022.10.14 |
| TIL 4์ผ์ฐจ (2022.10.12) (0) | 2022.10.12 |
| TIL 3์ผ์ฐจ (2022.10.11) (0) | 2022.10.11 |
| TIL 2์ผ์ฐจ (2022.10.07) (0) | 2022.10.07 |