๐โ๏ธ QNA ์๋น์ค ๊ธฐ๋ฅ ๊ตฌํ (๋ณต์ต) _ 92๊ฐ ~ 99๊ฐ
1๏ธโฃ ์ง๋ฌธ ์์ธ ํ์ด์ง
โ th:href ์ฌ์ฉ
- ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ๋ @{ } ์์ url ์ ์์ฑ
<a th:href="@{/question/list}" th:text="์ง๋ฌธ ๋ฆฌ์คํธ"></a>
- url ์์ ์๋ฐ ๊ฐ์ฒด ๊ฐ์ ๋ํด์ผํ ๋๋ @{ } ์ ๋ด์ฉ์ | | ๋ก ๊ฐ์ธ์ผํจ ex) ${question.id}
<a th:href="@{|/question/detail/${question.id}|}" th:text="${question.subject}"></a>
โก ๋ฐ์ดํฐ๊ฐ ์์ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ
@RequestMapping("/question/detail/{id}")
public String showQuestionDetail (@PathVariable int id, Model model) {
QuestionDetail questionDetail = questionService.getQuestionDetail(id);
if (questionDetail == null) { // ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ
throw new DataNotFoundException("question not found");
}
// (์๋ต)
}

- ํํ 1
public class DataNotFoundException extends RuntimeException {
// Throwable or Exception ์ ์์ ๋ฐ์ ๊ฒฝ์ฐ -> Throw & Catch ๋ฌธ ์ฌ์ฉํด์ผํจ
// RunTimeException -> ๋ฐํ์์ ์ข
๋ฃ๋๋ฏ๋ก Throw & Catch ๋ฌธ ์ฌ์ฉ ์ํจ
public DataNotFoundException(String message) {
super(message); // ์ค๋ฅ ๋ฉ์ธ์ง ์ถ๋ ฅ
}
}

- ํํ 2
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "entity not found") // ์ถ๊ฐ
public class DataNotFoundException extends RuntimeException {
public DataNotFoundException(String message) {
super(message);
}
}

์์ฑ์ ์ถ๊ฐํ ์๋ก ์ด๋ค ์ด์ ๋ก ์ค๋ฅ๊ฐ ๋ฐ์ํ๋์ง ์ฝ๊ฒ ํ์ ํ ์ ์๊ณ , ๋ง์ฝ ์ค๋ฅ๊ฐ ์ผ์ด๋ฌ๋๋ฐ DataNotFoundException ์ด ๋จ์ง ์๋๋ค๋ฉด ๋ค๋ฅธ ์ค๋ฅ๋ ์์ฌํด ๋ณผ ์ ์๋ค.
โข ๋ต๋ณ ๋ฑ๋ก
- Get ๊ณผ Post ์ ์ฐจ์ด : ํผ ์์ฑ ํ Method ๋ฅผ ๋ฐ๋ก ์ง์ ํ์ง ์์ผ๋ฉด GET ๋ฐฉ์์ผ๋ก ์์ฒญ, ๊ทธ๋ด ๊ฒฝ์ฐ Controller ์ ์ ๋ฌ๋๋ ์ธ์๊ฐ url ์ ๋ชจ๋ ๋ ธ์ถ๋จ.


- ๋ฑ๋กํ ๋ต๋ณ ๋ด์ฉ์ DTO ํํ๋ก ์ ์ฅ ํ Entity ๋ก ๋ณํํ์ฌ ์ ์ฅ
- ๋ฐ๋ก ์ ๋ฆฌ : https://like099.tistory.com/32
(Spring) DTO ์์ Entity ๋ณํ ํ ํ ์ด๋ธ ์ ์ฅ (ModelMapper ํ์ฉ)
๐ ๊ณต๋ถํ๊ฒ ๋ ๊ณ๊ธฐ QNA ํ๋ก์ ํธ๋ฅผ ๊ณต๋ถํ๋ฉด์ DTO ๋ฅผ ๋ง์ด ํ์ฉํด๋ณด๋ ค๊ณ ํ๋๋ฐ, ๋ต๋ณ ์ ์ฅ ๊ณผ์ ์์ ์ ๋ ฅ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ DTO ํํ๋ก ์ ์ฅํ๋ ๊ฒ ๊น์ง๋ ์ ๋ฒ์ ์ ๋ฆฌํ Entity ๋ฅผ DTO ํํ๋ก ๋ฐ๊พธ
like099.tistory.com
๐ ์ถ๊ฐ๋ก ๊ณต๋ถํ ๋ด์ฉ
DTO ์ ๋ํด ๊ณต๋ถํ๋ฉด์ ๊ทธ๋ ๋ค๋ฉด, DTO ์ ์ฌ์ฉ ๋ฒ์๋ ์ด๋์ธ๊ฑธ๊น? ์ ๋ํ ์๊ฐ์ด ๋ค๊ธฐ ์์ํ๋ค. ๋๋ ๊ฐ์ ๊ณ ๋ฏผ์ ํด๋ณด์ ๋ถ์ด ๊ณ์ ์ ์ฐธ์กฐํ ๊ธ์ ๋จ๊ธฐ๋ ค๊ณ ํ๋ค.
https://xlffm3.github.io/spring%20&%20spring%20boot/DTOLayer/
[Tecoble] DTO์ ์ฌ์ฉ ๋ฒ์์ ๋ํ์ฌ
DTO๋ ์ด๋ ๋ ์ด์ด๊น์ง ์ฌ์ฉํด์ผ ํ ๊น?
xlffm3.github.io
'TIL ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| TIL 8์ผ์ฐจ (2022.10.18) (0) | 2022.10.18 |
|---|---|
| TIL 7์ผ์ฐจ (2022.10.17) (0) | 2022.10.17 |
| TIL 5์ผ์ฐจ (2022.10.13) (0) | 2022.10.13 |
| TIL 4์ผ์ฐจ (2022.10.12) (0) | 2022.10.12 |
| TIL 3์ผ์ฐจ (2022.10.11) (0) | 2022.10.11 |