๋ฐ์ํ
[ Controller ์์ฑ ]
์์ฒญ์ ๋ฐ๋ฅธ ์ ์ ํ ์๋ต์ ๋ณด๋
@RequiredArgsConstructor
- ํ๋ ์ฃผ์
(@Autowired) ๋์ ์ฌ์ฉํ๋ ์์ฑ์ ์ฃผ์
!
- ์ํ ์ฐธ์กฐ ๋ฐฉ์ง
- ํ ์คํธ ์ฝ๋ ์์ฑ ์ฉ์ด
- ๊ฐ์ฒด ๋ณ์ด ๋ฐฉ์ง ( final ํค์๋ ์ฌ์ฉ )
- More: https://zorba91.tistory.com/238
@RequestBody
- ํด๋ผ์ด์ธํธ๊ฐ ์ ์กํ๋ Json ํํ์ HTTP Body ๋ด์ฉ์ Java Object๋ก ๋ณํ
- ๋ฉ์ธ์ง๋ฅผ ๋ณํํ๋ ๊ณผ์ ์์ ๊ฐ์ฒด์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ํตํด ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , ๋ด๋ถ์ ์ผ๋ก Reflection์ ์ฌ์ฉํด ๊ฐ์ ํ ๋น → ๊ธฐ๋ณธ ์์ฑ์๋ ํ์ํ๊ณ Setter๋ ํ์ํ์ง ์์ ์ด์ !
@PathVariable
- URI ๊ฒฝ๋ก์ ์ผ๋ถ๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ์ฌ์ฉํ ๋ ์ฌ์ฉ
- @RequestParam : Query String ์์ ๊ฐ์ ๊ฐ์ ธ์ฌ ๋ ์ฌ์ฉ
@Valid
- @RequestBody ๊ฐ์ฒด ๊ฒ์ฆ
- ์์กด์ฑ
- implementation 'org.springframework.boot:spring-boot-starter-validation’
[ Service ์์ฑ ]
SaveTodo
- ์ ๋ฌ๋ฐ์ requsetDto๋ก Todo Entity์์ฑ
- User์ Todo์ ์ค๊ฐ Entity์ธ UserTodo Entity์์ฑ
- ์์ฑ๋ Todo์ UserTodo ์ฐ๊ฒฐ
- Todo Entity ์ ์ฅ → UserTodo ๋ํ ์๋์ผ๋ก ์ ์ฅ ๋จ
[ Controller ํ ์คํธ ]
MockMVC
- ์์ฒญ์ ์ํํ๊ณ ์๋ต์ ๋ง๋ค์ด๋ด๋ Servlet API ๋ชจ์กฐ(mock) ๊ฐ์ฒด
- MockMvc ์ฃผ์ : @SpringBootTest + @AutoConfigureMockMvc
mockMvc.perform(post("/api/todo")
.content(asJsonString(reqDto))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
// then
.andExpect(status().isOk());
- “/api/todo” ๊ฒฝ๋ก๋ก post ์์ฒญ์ ๋ณด๋
- Content-Type: Json
- ์์๋๋ ์๋ต ์ํ(status) OK(200) ์ค์
๋ฐ์ํ
'๐ป ๊ฐ๋ฐ ์ผ์ง > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SpringBoot - Slack Bot ์ฐ๋ํ๊ธฐ (0) | 2022.04.05 |
---|---|
[Error] org.mockito.exceptions.misusing.UnnecessaryStubbingException: Unnecessary stubbings detected. (0) | 2022.03.27 |
[Test] SpringBoot Test - Service (Mockito) (0) | 2022.03.27 |
[22.03.09] QueryDSL (0) | 2022.03.09 |
[22.03.03] Entity ์์ฑ ๋ฐ Repository ํ ์คํธ (0) | 2022.03.07 |