Spring

Spring

[Spring] ํ•ด์‹œํƒœ๊ทธ ๊ธฐ๋Šฅ DB + JPA ์„ค๊ณ„

์บก์Šคํ†ค ์„ค๊ณ„๋ฅผ ํ•˜๋ฉด์„œ ํ•ด์‹œํƒœ๊ทธ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ์„ค๊ณ„๋ฅผ ํ•ด์•ผ ํ• ์ง€ ๊ณ ๋ฏผ์ด ๋์—ˆ๋‹ค..๐Ÿค” ๋‚ด๊ฐ€ ๊ณ ๋ฏผํ–ˆ๋˜ ๊ฒƒ ๐Ÿ”ธ ๊ธ€์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•ด์‹œํƒœ๊ทธ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ๐Ÿ”ธ ํ•ด์‹œํƒœ๊ทธ๋Š” ์—ฌ๋Ÿฌ ๊ธ€์— ์‚ฌ์šฉ ๋  ์ˆ˜ ์žˆ๋‹ค. ๐Ÿ‘‰ ๊ทธ๋Ÿผ DB ์ˆ˜์—… ๋“ค์—ˆ์„ ๋•Œ์ฒ˜๋Ÿผ N:M ๊ด€๊ณ„๋ฅผ ์จ์•ผ ํ•˜๋‚˜...?๋ผ๊ณ  ๊ณ ๋ฏผํ–ˆ์ง€๋งŒ N:M๊ด€๊ณ„๋Š” ์ ˆ๋Œ€์ ˆ๋Œ€์ ˆ๋Œ€ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค๊ณ  ํ•ด์„œ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ดค๋‹ค! โœ๏ธ ๋‹ค๋Œ€๋‹ค๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๋„๋ฉ”์ธ ๋ชจ๋ธ์ด ๋‹จ์ˆœํ•ด์ง€๊ณ  ํŽธ๋ฆฌํ•œ ์žฅ์ ์ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์—”ํ‹ฐํ‹ฐ์— ์—ฌ๋Ÿฌ ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋Š”๋ฐ ์ถ”๊ฐ€ํ•œ ์นผ๋Ÿผ๋“ค์€ ๋งคํ•‘ํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ํ•œ๋‹ค..! ๊ฒฐ๊ตญ @ManyToMany๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•จ.. ํ•ด๊ฒฐํ•œ ๋ฐฉ๋ฒ• ๐Ÿ”ธ ์ผ๋Œ€๋‹ค, ๋‹ค๋Œ€์ผ ๊ด€๊ณ„๋ฅผ ๋งŒ๋“ค์ž! ๐Ÿ”ธ ๋งคํ•‘ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋งŒ๋“ค์–ด ๊ธ€๊ณผ ๋งคํ•‘์—”ํ‹ฐํ‹ฐ๋Š” ์ผ๋Œ€๋‹ค, ๋งคํ•‘์—”ํ‹ฐํ‹ฐ์™€ ํ•ด์‹œํƒœ๊ทธ๋Š” ๋‹ค๋Œ€์ผ ๊ด€๊ณ„๋กœ ์„ค๊ณ„ํ•œ..

Spring

[Spring] MultipartFile, Dto ํ•œ๋ฒˆ์— ์ž…๋ ฅ ๋ฐ›๊ธฐ | swagger | S3

์ด๋ฏธ์ง€๋ž‘ dto๋ž‘ ๊ฐ™์ด ์ž…๋ ฅ ๋ฐ›๊ณ  ์‹ถ์€๋ฐ postman์—์„œ๋Š” multipartFile์€ ์ด๋ฏธ์ง€๋กœ dto๋Š” json์œผ๋กœ ๋”ฐ๋กœ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์Šค์›จ๊ฑฐ์—์„œ ์‹คํ–‰ํ• ๋•Œ๋Š” ํ•œ ๋ฒˆ์— ์•ˆ๋œฌ๋‹ค....๐Ÿ˜ญ ๊ทธ๋ž˜์„œ @RequestBody๋Œ€์‹ ์— @ModelAttribute๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ž…๋ ฅ ๋ฐ›๊ธฐ! Controller @PostMapping(value = "/write") public BaseResponse uploadPosting(@RequestPart MultipartFile image, @ModelAttribute PostingDto postingDto) throws IOException { try { postingService.uploadPosting(image, createPostingDto); return new Ba..

Spring

[Spring][Redis] RedisConnectionException : NOAUTH Authentication required ์—๋Ÿฌ

redis๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ec2์— ๋ฐฐํฌ๋ฅผ ํ•˜๋Š”๋ฐ io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication required.์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋–ด๋‹ค ์ฐพ์•„๋ณด๋‹ˆ๊นŒ redis.conf ์ˆ˜์ •ํ•  ๋•Œ ์„ค์ •ํ–ˆ๋˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๋ง! requirepass์— ์ ์—ˆ๋˜ ๋น„๋ฒˆ์„ spring ํ”„๋กœ์ ํŠธ application.yml์— ์ถ”๊ฐ€์‹œํ‚ค๋ฉด ๋œ๋‹ค! (local๋กœ ํ•  ๋•Œ๋Š” ๋น„๋ฒˆ์ด ํ•„์š”์—†์ง€๋งŒ ec2๋ฅผ ํ†ตํ•ด ์™ธ๋ถ€์—์„œ ์ ‘์†ํ•  ๊ฒฝ์šฐ์—๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์žˆ์–ด์•ผ ์ ‘์†์ด ๊ฐ€๋Šฅ) redis: port: 6379 host: password: ์œ„ password์— ์„ค์ •ํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ ‘์†ํ•˜๋ฉด ๋!

Spring

SpringBoot [Bean Validation]

Bean Validation์ด๋ž€? ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•˜๋Š” JAVA์˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ํ‘œ์ค€ ๊ธฐ์ˆ ์ด๋‹ค. Bean Validation์€ ํŠน์ •ํ•œ ๊ตฌํ˜„์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ Bean Validation 2.0์ด๋ผ๋Š” ๊ธฐ์ˆ  ํ‘œ์ค€์ด๋‹ค. ๊ฒ€์ฆ ์• ๋…ธํ…Œ์ด์…˜๊ณผ ์—ฌ๋Ÿฌ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ชจ์Œ์ด๋‹ค. โญ ์ผ๋‹จ bean validation์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ์˜์กด๊ด€๊ณ„๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค! Maven org.springframework.boot spring-boot-starter-validation Gradle implementation 'org.springframework.boot:spring-boot-starter-validation' ๐ŸŸช ์–ด๋…ธํ…Œ์ด์…˜ ์ •๋ฆฌ @NotBlank : ๋นˆ ๊ฐ’ + ๊ณต๋ฐฑ๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ @NotNull : null์„ ํ—ˆ์šฉํ•˜์ง€ ..

Spring

Thymeleaf ํƒ€์ž„๋ฆฌํ”„ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ ์ •๋ฆฌ [ํ…์ŠคํŠธ/๋ณ€์ˆ˜/๋ฐ˜๋ณต/์†์„ฑ]

ํƒ€์ž„๋ฆฌํ”„๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด๋‹ค. ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜๋Š” ํ™˜๊ฒฝ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค! ์—ฌ๊ธฐ์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ณณ์„ ํ†ตํ•ด์„œ ํ”„๋กœ์ ํŠธ๋Š” ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” Dependencies์— ํƒ€์ž„๋ฆฌํ”„๋ฅผ ์„ ํƒํ•˜๋ฉด๋œ๋‹ค. ํ˜น์‹œ ์„ ํƒํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด build.gradle์— ๋“ค์–ด๊ฐ€์„œ dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' } dependencies์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€์‹œ์ผœ ์ฃผ๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค!! ๊ทธ๋Ÿผ ํƒ€์ž„๋ฆฌํ”„ ๊ฐœ๋…์„ ์ •๋ฆฌํ•ด๋ด์•ผ๊ฒ ๋‹ค๐Ÿ™ƒ ํƒ€์ž„๋ฆฌํ”„ (Thymeleaf) ํƒ€์ž„๋ฆฌํ”„๋Š” ์„œ๋ฒ„ ์‚ฌ์ด๋“œ ์ž๋ฐ” ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด๋‹ค. โ—พ ์„œ๋ฒ„ ์‚ฌ์ด๋“œ HTML ๋ Œ๋”๋ง ๋ฐฑ์—”๋“œ ์„œ๋ฒ„์—์„œ HTML์„ ๋™์ ์œผ๋กœ ๋ Œ๋”๋ง ํ•˜๋Š” ์šฉ๋„์ด๋‹ค. โ—พ ๋‚ด์ถ”๋Ÿด ..

Spring

Spring MVC [ Model / @ModelAttribute]

spring์„ ๊ณต๋ถ€ํ•˜๋‹ค ๋ณด๋‹ˆ๊นŒ Model์ด๋ž‘ @ModelAttribute๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์ด ๋‚˜์™€์„œ ํ•œ๋ฒˆ ์ •๋ฆฌ ์ข€ ํ•˜๊ณ  ์‹ถ์–ด์„œ ๊ธ€ ์“ฐ๊ฒŒ ๋๋‹ค... Model์ด๋ž‘ @ModelAttribute๋ž‘ ๋‹ค๋ฅด์ง€๋งŒ ์ž๊พธ ํ—ท๊ฐˆ๋ ค์„œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ… ใ…  ๐Ÿ’œ Model ๐Ÿ’œ Model์€ ๊ฐ์ฒด์ด๋‹ค. view์˜ ์ด๋ฆ„๊ณผ view๋ฅผ ๋ Œ๋”๋งํ•  ๋•Œ ํ•„์š”ํ•˜๋‹ค. map์œผ๋กœ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— controller์—์„œ view์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ key, value๋กœ ๋„ฃ์–ด์ค€๋‹ค. Servlet์—์„œ request.setAttribute()์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•œ๋‹ค. model๊ฐ์ฒด์— ๋‹ด์„ ๋•Œ๋Š” model.addAttribute("key", value)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๐Ÿ’œ @ModelAttribute ๐Ÿ’œ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ๊ฐ์ฒด์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์–ด..

Spring

Spring MVC ๊ธฐ๋ณธ๊ธฐ๋Šฅ- HTTP ์š”์ฒญ [ ์š”์ฒญํŒŒ๋ผ๋ฏธํ„ฐ, ์š”์ฒญ๋ฉ”์‹œ์ง€]

๐Ÿ’ฌ ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋Š” ์ฃผ๋กœ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•œ๋‹ค. โ—พ GET - ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ /?username=hello&age=20 ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๋ฉ”์‹œ์ง€ ๋ฐ”๋”” ์—†์ด, url์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์„œ ์ „๋‹ฌํ•œ๋‹ค. ๊ฒ€์ƒ‰, ํ•„ํ„ฐ, ํŽ˜์ด์ง•์—์„œ ๋งŽ์ด ์‚ฌ์šฉ โ—พ POST - HTML Form content-type : application/x-www-form-urlencoded ํ˜•์‹ ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌ (username=hello&age=20) ํšŒ์›๊ฐ€์ž…, ์ƒํ’ˆ์ฃผ๋ฌธ์—์„œ ๋งŽ์ด ์‚ฌ์šฉ โ—พ HTTP message body์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋‹ด์•„์„œ ์š”์ฒญ HTTP API์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ (JSON, XML, TEXT) ๋ฐ์ดํ„ฐ ํ˜•์‹์€ ์ฃผ๋กœ JSON์‚ฌ์šฉ POST, PUT, ..

Spring

Spring [@RequestMapping ๊ธฐ์ดˆ ์ •๋ฆฌ ]

์Šคํ”„๋ง ์ž…๋ฌธ ๊ฐ•์˜, ๊ธฐ์ดˆ๊ฐ•์˜๋ฅผ ๋“ค์—ˆ์„ ๋•Œ ์ข…์ข… ๋ณด์•˜๋˜ RequestMapping์— ๋Œ€ํ•œ ๊ธฐ์ดˆ๋ฅผ ๋‹ค์‹œ ๋‹ค์กŒ๋‹ค! ์•„์ง ๊ธฐ์ดˆ๋งŒ ๋ฐฐ์šด ๊ฑฐ๋ผ ์‹ค๋ฌด์—์„œ ์–ด๋””์— ์‚ฌ์šฉํ• ์ง€ ๊ฐ๋ฐ–์— ์˜ค์ง€ ์•Š์ง€๋งŒ ์–ธ์  ๊ฐ„ ๋Šฅ์ˆ˜๋Šฅ๋ž€ํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ณ  ์‹ถ๋‹ค๐Ÿ˜š RequestMapping(์š”์ฒญ ๋งคํ•‘) ์ด๋ž€? ์š”์ฒญ์ด ์™”์„ ๋•Œ ์–ด๋–ค ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ํ˜ธ์ถœ์ด ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ์ง€ํ‘œ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. ๐Ÿ‘‰ ์˜ˆ์‹œ @RequestMapping(value = "/hello-basic") ์ด๋ ‡๊ฒŒ ๋งคํ•‘์„ ํ•˜๋ฉด localhost:8080/hello-basic์œผ๋กœ url์„ ์ž…๋ ฅํ–ˆ์„ ๊ฒฝ์šฐ์— ์ด๊ฒƒ์— ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. @RequestMapping์€ ๋‹ค์ค‘์š”์ฒญ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค! ๋‹ค์ค‘ ์š”์ฒญ์„ ํ•˜๋ ค๋ฉด ๋ฐฐ์—ด๋กœ ๋ฌถ์–ด์•ผ ํ•œ๋‹ค. @RequestMapping(value = {"/hello", "/..

Spring

Spring boot - ๋กœ๊น… [ ์Šคํ”„๋ง ๋กœ๊ทธ๋‚จ๊ธฐ๊ธฐ ]

์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋กœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ž๋™์ ์œผ๋กœ ์ถ”๊ฐ€๋œ๋‹ค. ๋กœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ข…๋ฅ˜๊ฐ€ ๋งŽ์€๋ฐ ๊ทธ๊ฒƒ์„ ํ†ตํ•ฉ์ ์œผ๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด SLF4J ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. ์‹ค๋ฌด์—์„œ๋Š” Logback์„ ๋Œ€๋ถ€๋ถ„ ์‚ฌ์šฉํ•œ๋‹ค. ๐Ÿ’ฌ ๋กœ๊ทธ ์„ ์–ธ ๋ฐฉ๋ฒ• // 1๋ฒˆ ๋ฐฉ๋ฒ• private Logger log = LoggerFactory.getLogger(getClass()); // 2๋ฒˆ ๋ฐฉ๋ฒ• private static final Logger log = LoggerFactory.getLogger(Xxx.class); //3๋ฒˆ ๋ฐฉ๋ฒ• ์• ๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ(lombok์ด ์ œ๊ณต) @Slf4j โœ ์ฒซ๋ฒˆ์งธ ๋‘ ๋ฒˆ์งธ ๋กœ๊ทธ ์„ ์–ธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋กœ๊ทธ๋Š” slf4j๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. ๐Ÿ“ ๋กœ๊ทธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ• @Slf4j // lombok์ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ..

Spring

Spring ์„œ๋ธ”๋ฆฟ [HttpServletRequest/HttpServletResponse]

spring mvc๊ฐ•์˜๋ฅผ ๋“ฃ๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค! mvc๋ฅผ ๋ฐฐ์šฐ๊ธฐ ์ „์— ์„œ๋ธ”๋ฆฟ๋ถ€ํ„ฐ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๋Š”๋ฐ ์ž˜ ์ดํ•ดํ•œ ๊ฑด์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋‹ค...! ์ด๋ฒˆ์—๋Š” ์„œ๋ธ”๋ฆฟ์„ ์ด์šฉํ•ด์„œ ์‚ดํŽด๋ณธ ์š”์ฒญ๊ณผ ์‘๋‹ต ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•ด๋ณด๊ณ  ์ž‘๋™ ์›๋ฆฌ๋ฅผ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ๐ŸŒ  ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ ์„œ๋ธ”๋ฆฟ์„ ์ง์ ‘ ๋“ฑ๋กํ•ด์„œ ์“ฐ๋ ค๋ฉด ์–ด๋…ธํ…Œ์ด์…˜์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. @ServletComponentScan //์„œ๋ธ”๋ฆฟ ์ž๋™ ๋“ฑ๋ก [ ์„œ๋ธ”๋ฆฟ ๋“ฑ๋กํ•˜๊ธฐ ] @WebServlet(name="", urlPatterns = "/") name : ์„œ๋ธ”๋ฆฟ ์ด๋ฆ„ urlPatterns : URL ๋งคํ•‘ [ HttpServletRequest ] ๋จผ์ € http ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ์‚ดํŽด๋ณด์ž๋ฉด. POST /save HTTP/1.1 Host: localhost:8080 Content-Type: app..

DAHLIA CHOI
'Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (3 Page)