Framework/Springboot

[Springboot] Postman Rest API ์š”์ฒญ Getting not supported media type error / 415 Unsupported MediaType

yuri lee 2023. 2. 1. 23:43
๋ฐ˜์‘ํ˜•

Intro

์•ˆ๋…•ํ•˜์„ธ์š”. ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ Api ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด postman ์„ ์‚ฌ์šฉํ•˜๋˜ ๋„์ค‘ ๋‹ค์Œ์˜ ์• ๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

 

415 Unsupported MediaType 

 

์ฐธ๊ณ ๋กœ, ์ €๋Š” ์ปจํŠธ๋กค๋Ÿฌ ๋‹จ์—์„œ @PostMapping๊ณผ @RequestBody ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ–ˆ๊ณ , Put ์š”์ฒญ์œผ๋กœ ์•„๋ž˜ ๋‚ด์šฉ์„ ์ „๋‹ฌํ•ด ์—…๋ฐ์ดํŠธ๋ฅผ ์‹œ์ผœ์ค˜์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

{
    "isOwner": true,
    "isStaff": false,
}

 

Why?

์ง€์›ํ•˜์ง€ ์•Š๋Š” ๋ฏธ๋””์–ด ํƒ€์ž…์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์†Œ์Šค ์ž์ฒด์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ์ค„ ์•Œ๊ณ , ์—ด์‹ฌํžˆ ๊ตฌ๊ธ€๋ง ํ–ˆ์ง€๋งŒ ๋ฌธ์ œ๋Š” ๋”ฐ๋กœ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 

 

How to solve the problem

์ฒ˜์Œ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด Postman > Body ํƒญ >  raw๋ฅผ ์„ ํƒ ํ›„ ํ•ด๋‹น json ํŒŒ์ผ์„ ๋„ฃ์–ด ์š”์ฒญ์„ ํ•ด์คฌ๋Š”๋ฐ์š”, ์•Œ๊ณ ๋ณด๋‹ˆ ์˜† ๋“œ๋กญ๋‹ค์šด ๋ฉ”๋‰ด์—์„œ JSON (application/json)์„ ์„ ํƒ์„ ์•ˆํ•ด์คฌ๋”๋ผ๊ณ ์š”. 

 

Json ์„ ํƒ ํ›„ ๋‹ค์‹œ ์š”์ฒญํ•˜๋‹ˆ 415 ์—๋Ÿฌ ์—†์ด ์ž˜ ๋™์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค :)

 

Conclusion

API ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์•„์ง๋„ ๋ญ”๊ฐ€ ๋‚ฏ์„ค๊ณ  ์–ด๋ ต๋„ค์š”.. ๐Ÿ˜ญ

๋ฐ˜์‘ํ˜•