๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/๐Ÿƒ Spring

Spring | @RequestMapping

  • -

RequestMapping

์š”์ฒญ์ด ์™”์„ ๋•Œ ์–ด๋–ค ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ํ˜ธ์ถœ์ด ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ์ง€ํ‘œ

 

@RequestMapping์˜ value๋ฅผ "/hello"๋กœ ์ •์˜ํ•ด์ฃผ๋ฉด localhost:8080/hello ์ฃผ์†Œ ์ž…๋ ฅ์‹œ /hello์˜ ๋ฉ”์†Œ๋“œ ์‹คํ–‰
@RequestMapping(value="/hello")

 

@RequestMapping์˜ ๋‹ค์ค‘ ์š”์ฒญ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ ๋ฐฐ์—ด๋กœ ๋ฌถ์–ด์„œ ์‚ฌ์šฉ. ๋‹ค์ค‘ ์š”์ฒญ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋‹ค์ค‘ url ์ค‘ ์–ด๋–ค url์„ ์‚ฌ์šฉํ•ด๋„ ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
@RequestMapping(value={"/hello", "/hello-world"})

 

@RequestMapping๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ํšจ์œจ์ ์ด์ง€ ๋ชปํ•˜๋‹ค.
@RestController
public class HelloController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String helloGet(...) {
        ...
    }

    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public String helloPost(...) {
        ...
    }

    @RequestMapping(value = "/hello", method = RequestMethod.PUT)
    public String helloPut(...) {
        ...
    }

    @RequestMapping(value = "/hello", method = RequestMethod.DELETE)
    public String helloDelete(...) {
        ...
    }
}

 

 

ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•œ ๊ฒƒ์ด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ์ด๋‹ค.
GET, POST ๋“ฑ ๋ฉ”์†Œ๋“œ์— url๋ฅผ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. (@GetMapping("/hi")) | ์ฃผ์†Œ : /hello/hi
@RestController
@RequestMapping(value = "/hello")
public class HelloController {

    @GetMapping()
    public String helloGet(...) {
        ...
    }

    @PostMapping()
    public String helloPost(...) {
        ...
    }

    @PutMapping()
    public String helloPut(...) {
        ...
    }

    @DeleteMapping()
    public String helloDelete(...) {
        ...
    }
}

 

'๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก > ๐Ÿƒ Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Spring MVC | Web Application  (0) 2023.01.13
Spring Boot/VSC | New Project  (0) 2023.01.10
Spring/Mybatis | foreach(bulk insert)  (0) 2023.01.09
Spring/Mybatis | keyProperty  (0) 2023.01.09
Spring/Mybatis | ๊ณ ์ฐฐ #{}  (0) 2023.01.03
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.