1 minute read

221024 TIL

Mention : Winter is coming๐Ÿฅถ ์•„์นจ์— ์ถฅโ€ฆ.์ถฅ๋‹ค! ์‹œ๊ฐ„๋‚  ๋•Œ, ๊นƒ๋ธ”๋กœ๊ทธ๋กœ ์ „ํ™˜ํ•ด์•ผ๊ฒ ๋‹ค๐Ÿ’ก

Acheivement & Problem : ์–ด์ œ ๊ตฌํ˜„ํ–ˆ๋˜ AWS S3๋ฅผ ์ด์šฉํ•œ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ๊ธฐ๋Šฅ์„ ์‹ค์ œ ํ”„๋กœ์ ํŠธ์— ์ ์šฉ & ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

์–ด์ œ๋Š” MultipartFile๋งŒ Body์— ๋ฐ›์•„์„œ Post API๋ฅผ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ, ํ”„๋กœ์ ํŠธ์—์„œ๋Š” JSON ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋„ ๋ฐ›์•„์„œ

Post API๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ–ˆ๋‹ค. ๊ฒŒ์‹œ๊ธ€์˜ ์ œ๋ชฉ, ๋‚ด์šฉ, ์œ ์ €๋„ค์ž„์€ JSON MediaType, ์ด๋ฏธ์ง€ํŒŒ์ผ์€ MULTIPART MediaType ์ด๋Ÿฐ ๊ตฌ์กฐ์ด๋‹ค.

์—๋Ÿฌ์˜ ์›์ธ์€ ๋นจ๋ฆฌ ํŒŒ์•…ํ•œ ๊ฑฐ ๊ฐ™์€๋ฐ, ์ ์šฉ์ค‘ Configure Bean? ์ ์–ด๋†“์ง„ ์•Š์•˜๋Š”๋ฐ ์ด๋Ÿฌํ•œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ๐Ÿคฏ

๊ธฐ์กด ์ž๋ฐ” ํด๋ž˜์ŠคํŒŒ์ผ์„ ์ง€์šฐ๊ณ  ๋‹ค์‹œ ์“ฐ๊ณ  ํ•˜๋Š” ๊ณผ์ •์ค‘์— not founded ์—๋Ÿฌ๋กœ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์ธ๋“ฏ ์‹ถ๋‹ค.

Gradle์— ๋“ค์–ด๊ฐ€์„œ Clean -> Build๋ฅผ ํ•ด์ฃผ๋ฉด ๊น”๋”ํ•˜๊ฒŒ ์ฒญ์†Œ๋œ๋‹ค. ์–ด์งธ์ €์งธ ํ•ด๊ฒฐํ•˜๊ณ  ๋‘ ๊ฐ€์ง€ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์„œ

๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ณ  Postman ํ…Œ์ŠคํŠธ & AWS S3 Bucket์— ์ €์žฅ๋˜๋Š” ๊ฒƒ ๊นŒ์ง€ ํ™•์ธ์™„๋ฃŒ๐Ÿ”ฅ

ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๊นŒ์ง€ ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„ & HTTP ์— ๋Œ€ํ•ด์„œ ์ •ํ™•ํžˆ ์•Œ์•„๋‘ฌ์•ผ ์˜ค๋Š˜๊ณผ ๊ฐ™์€ ์—๋Ÿฌ์˜ ๊ทผ๋ณธ ์›์ธ์„ ๋น ๋ฅด๊ฒŒ

ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค๐Ÿ› 


์˜ค๋Š˜ ๊ตฌํ˜„ํ–ˆ๋˜, API์—์„œ Json๊ณผ MultipartFile์„ ํ•œ๋ฒˆ์— ์ „๋‹ฌ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ API์—์„œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ @RequestBody๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ, Multipartfile์€ ๋ฏธ๋””์–ดํƒ€์ž…์ด ๋‹ฌ๋ผ @RequestBody๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜๊ฐ€ ์—†๋‹ค.

@RequestPart๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Json & MultipartFile ๋ฏธ๋””์–ด ํƒ€์ž…(ํŒŒ์ผ)์„ ๋‘˜ ๋‹ค ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ API์—์„œ consumeํ•  MediaType์„ ์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์ง€์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ์ ์ ˆํ•œ MediaType๋ฅผ ์ง€์ •ํ•˜์ง€

์•Š์„ ๊ฒฝ์šฐ 415 Unsupported MediaType ERROR์™€ ์ธ์‚ฌํ•˜๊ฒŒ ๋œ๋‹ค๐Ÿคฏ

@PostMapping(value = "/api/article", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
    public ResponseEntity<?> createArticle(@RequestPart ArticleRequestDto requestDto, @RequestPart MultipartFile multipartFile) throws IOException {
        return ResponseEntity.ok(articleService.createArticle(requestDto, multipartFile));
    } 

Leave a comment