Spring์ URL๋ก ๋ค์ด์จ ์์ฒญ์ Mapping๋ URL์ ์ฐพ์ ์ฒ๋ฆฌํ๋ค. ์ด๋ URL์ Mapping ํ๋ Annotation์ด @RequestMapping๊ณผ ๊ทธ๋ก๋ถํฐ ํ์๋ @Get, @Post, @Put, @Update,.. Mapping์ด๋ค.
๊ธฐ์กด ํํ๋ @RequestMapping(value = "test", method= RequestMethod.GET) ์ ํํ๋ฅผ ๊ฐ์ง๊ณ ์์์ง๋ง Spring 4.3๋ฒ์ ๋ถํฐ Method Mapping์ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ฉฐ ์ฝ๋ ๊ฐ๋ ์ฑ์ด ๋์์ก๋ค.
@RequestMapping์ ๊ฒฝ์ฐ HTTP ์์ฒญ๊ณผ ์ปจํธ๋กค๋ฌ์ ๋ฉ์๋๋ฅผ Mapping ํ๊ธฐ์ํด ์ฌ์ฉ๋๋ค. ๋ค์ํ ์์ฑ์ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ HTTP ๋ฉ์๋, ๊ฒฝ๋ก, ์์ฒญ ํค๋, ์์ฒญ ๋งค๊ฐ๋ณ์ ๋ฑ์ ๊ธฐ๋ฐ์ผ๋ก ์์ฒญ์ Mapping ํ ์ ์๋ค.
@RequestMapping์ ๊ฒฝ์ฐ Class์ Method์ ์ ์ฉํ ์ ์๋ค. value๋ฅผ ์ฌ์ฉํด url์ ์ง์ ํ ์ ์๊ณ , method๋ฅผ ์ฌ์ฉํด ์ ์ฉ๋ method๋ฅผ ์ง์ ํ ์ ์๋ค.
๊ธฐ๋ณธ์ ์ธ Mapping ๋ฐฉ๋ฒ
@RequestMapping(value = "/test",method = RequestMethod.GET) public String testView(){ return "test"; }
๋ฉ์๋์ value์ method๋ฅผ ์ฌ์ฉํ URL Mapping์ด๋ค. GET ๋ฐฉ์์ผ๋ก ์๋๋๋ฉฐ, "/test" URL๋ก ์ ๊ทผ์ ์๋๋๋ค. ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ Method๋ ์ ์ฉ์ด ๊ฐ๋ฅํ๋ฐ, POST, PUT, PATCH, DELETE ๋ฑ ์ฃผ๋ก ์ฌ์ฉ๋๋ 5๊ฐ์ง์ Method๊ฐ ์๋ค.
Class์ URL์ ์ง์ ํ์ฌ ๋ฉ์๋์ ๊ณตํต URL path ์ค์
@Controller @RequestMapping("/page") public class Test{ @RequestMapping(value = "/test", method = RequestMethod.GET) public String a(){ return "test"; } }
ํด๋์ค์ ์ ์ฉํ์ฌ URL path๋ฅผ ์ง์ ํ๊ณ , ํ์ ๋ฉ์๋์๋ "/test" URL์ Mappingํ์ฌ "/page/a" ์๋ํ URL๊ฒฝ๋ก๋ฅผ ์ ์ฉํ์๋ค. ์ด๋ ๋ฏ ํด๋์ค ๋๋ ๋ฉ์๋์ ์ ์ฉ์์น์ ๋ฐ๋ผ ์ ์ฉ์ด ๋ค๋ฅด๊ฒ ๋๋ค.
์ฌ๋ฌ๊ฐ์ URL์ ํ๋์ ๋ฉ์๋์ Mapping
@RequestMapping({"test1","test2"}) public String test(){ return "test"; }
value์ {}์ ๊ฐ์ ๋ฐฐ์ด์ ์ธ์ ํตํด ์ฌ๋ฌ๊ฐ์ URL์ ํ๋์ ๋ฉ์๋์ Mapping ํ ์๋ ์๋ค.
๊ฒฝ๋ก ๋ณ์ ์ฌ์ฉ
@RequestMapping("/test/{id}") public String test( @PathVariable String id ){ return "test"; }
@PathVariable์ ์ฌ์ฉํ URL ๊ฒฝ๋ก ๋ณ์ ์ฌ์ฉ๋ ๊ฐ๋ฅํ๋ค.
ํ๋ผ๋ฏธํฐ ์กฐ๊ฑด Mapping
@RequestMapping(value = "/test", method = RequestMethod.GET, params = "test=true") @ResponseBody public String test() { return "test"; }
ํ๋ผ๋ฏธํฐ ์กฐ๊ฑด๊ฐ์ด ๋น์ด์๋ ๊ฒฝ์ฐ 400์๋ฌ๊ฐ ๋ฐ์ํ์ง๋ง
ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ์ ์ ์กฐ๊ฑด์์๋ ์ ์๋๋๋ค.ํค๋ ์กฐ๊ฑด Mapping
@RequestMapping(value = "/test", method = RequestMethod.GET, headers = "test=true") @ResponseBody public String test() { return "test"; }
ํค๋์ ๊ฐ์ด ์์๊ฒฝ์ฐ 404์๋ฌ๊ฐ ๋ฐ์ํ์ง๋ง
test = true ๊ฐ ์์๊ฒฝ์ฐ ๊ฐ์ ๋ฐํํ๋ค.๋ฏธ๋์ดํ์ ์ ๋ฐ๋ฅธ ์กฐ๊ฑด Mapping
@GetMapping(value = "/test", consumes = "application/json") @ResponseBody public String test() { return "test"; }
๋ฏธ๋์ด ํ์ ์ด Mapping ์กฐ๊ฑด์ ๋ฏธ๋์ดํ์ ๊ณผ ๋ฌ๋ฆฌ 415์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
๋์ผํ ๊ฒฝ์ฐ ์ ์ ์๋ํ๋ค.
Method Mapping์ ๊ฒฝ์ฐ Spring 3.4๋ฒ์ ๋ถํฐ ์ถ๊ฐ๋ Mapping ์ด๋ ธํ ์ด์ ์ด๋ค. ๊ธฐ์กด @RequestMapping์ method ๋ถ๋ถ๋ง์ ์ฌ์ฉํ์ฌ "@GetMapping" ์ด์ ๊ฐ์ ํํ๋ก ์์ฑํ๋ค. RequestMapping๊ณผ ๋ค๋ฅธ์ ์ ํด๋์ค์ ์ ์ฉ์ด ๋ถ๊ฐ๋ฅํ๋ค. ์ข ๋ฅ๋ ๋ง์ง๋ง ์ค์ ๋ก ์๋ํ๋ ๋ฐฉ์์ GetMapping์ ์ ์ธํ๊ณ ๋ ๋ชจ๋ ๋์ผํ๋ค. ํ์ง๋ง RestAPI๋ฅผ ๋ง๋ค๊ธฐ์ํด์ ์ ์ ํ๊ฒ ๊ธฐ๋ฅ์ ๋ง๋ Mapping์ ์ฌ์ฉํด์ผํ๋ค.
@GetMapping("/test") public User test(){ return "test"; }
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ Mapping์ธ GetMapping์ด๋ค. ํต์์ ์ผ๋ก URL ์ด๋์ ์ํด ์ฌ์ฉํ๋ค. ๋ฐ์ดํฐ ์ ์ก์ URL์ ์ด์ฉํ ํ๋ผ๋ฏธํฐ๋ก ์ ์กํ๊ธฐ ๋๋ฌธ์ ๋ณด์์ด ์ทจ์ฝํ๋ค. ๋ฆฌ์์ค์ ์กฐํ๋ฅผ ์ํด ์ฌ์ฉํ๋ค.
@PostMapping("/test") public User test(){ return "test"; }
PostMapping, Server์ ๋ฆฌ์์ค๋ฅผ ์์ฑํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ค. ํํ ๋ก๊ทธ์ธ, ํ์๊ฐ์ , form์ ์ด์ฉํ ๊ฒ์๊ธ ์์ฑ ๋ฑ์ ์ฌ์ฉ๋๋ฉฐ, ๋ฐ์ดํฐ๊ฐ Header์ ๋ณธ๋ฌธ์ ํตํด ์ ์ก๋๊ธฐ ๋๋ฌธ์ Get๋ฐฉ์์ ๋นํด ์๋์ ์ผ๋ก ๋ณด์์ด ๊ฐํ๋ค๋ ๋ถ๋ถ์ด ์๋ค.
@PutMapping("/test") public User test(){ return "test"; }
PutMapping, Server์ ๋ฆฌ์์ค๋ฅผ ์์ ํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ค. ๋ฐ์ดํฐ ์ ์ก๋ฐฉ์์ Post์ ๋์ผํ๊ฒ Header์ ๋ณธ๋ฌธ์ ํตํด ์ ๋ฌ๋๋ค.
@PatchMapping("/test") public User test(){ return "test"; }
PatchMapping, Server์ ๋ฆฌ์์ค ์ผ๋ถ๋ฅผ ์์ ํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ค. Put๊ณผ ๋ค๋ฅธ์ ์ ์ผ๋ถ ์์ ํ๋ค๋๊ฒ์ ์๋ค. ๋ฐ์ดํฐ ์ ์ก๋ฐฉ์์ Post์ ๋์ผํ๊ฒ Header์ ๋ณธ๋ฌธ์ ํตํด ์ ๋ฌ๋๋ค.
@DeleteMapping("/test") public User test(){ return "test"; }
DeleteMapping, ์ด๋ฆ ๊ทธ๋๋ก Server์ ๋ฆฌ์์ค๋ฅผ ์ญ์ ํ ๋ ์ฌ์ฉ๋๋ค. ๋ฐ์ดํฐ ์ ์ก ์ญ์ Post์ ๋ง์ฐฌ๊ฐ์ง๊ณ Header์ ๋ณธ๋ฌธ์ ํตํด ์ ์ก๋๋ค.
๋ธ๋ผ์ฐ์ ์์ ์์ฒญ์ ๋ณด๋ด๋ ๋ฐฉ์์ ๊ธฐ๋ณธ์ ์ผ๋ก Get, Post ๋ฐฉ์์ผ๋ก ์ ์ฉ๋๋ค. ๋ง์ฝ ๋ค๋ฅธ Put, Patch, Delete ์ method๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ํ์ํ๋ค.
<form action="/test" method="POST"> <input type="hidden" name="_method" value="PUT"> <button type="submit">์ ์ก</button> </form>
form ํ๊ทธ๋ POST๋ก ์ง์ ํ๊ณ ๋ด๋ถ input type hidden ํ๊ทธ๋ฅผ ์์ฑ ํ name์ "_method" ๋ก ์ง์ , value="PUT"์ผ๋ก ํ๋ฉด Server์์ ํด๋น ๊ฐ์ ์ฝ๊ณ PUTMapping์ผ๋ก ์ฒ๋ฆฌํ๋ค.
<script> function testPutRequest() { var xhr = new XMLHttpRequest(); xhr.open('PUT', '/test', true); xhr.send(); } </script>
ajax๋ฅผ ์ฌ์ฉํ์ฌ method ๋ฐฉ์์ ์ง์ ํ๊ณ ํด๋น ์์ฒญ์ ๋ณด๋ด๋ฉด method์ ๋ง๊ฒ ์ฒ๋ฆฌ๋๋ค.