๐ก ์์ฒญ ๋งคํ์ด๋?
- url ์์ฒญ์ด ์์ ๋ ์ด๋ค ์ปจํธ๋กค๋ฌ๊ฐ ํธ์ถ๋์ด์ผ ํ๋์ง ์ ํด๋๋ ๊ฒ์ ๋งํ๋ค.
โ๏ธ ์ฌ์ฉ๋ฒ
- @RequestMapping์ ํตํด ๋ช ์๋ url ๊ฒฝ๋ก๋ก ์์ฒญ์ด ๋ค์ด์ฌ ๊ฒฝ์ฐ ํด๋น ๋ฉ์๋๊ฐ ํธ์ถ๋๋ค.
- ๋จ์ผ, ๋ค์ค ๋งคํ์ด ๊ฐ๋ฅํ๋ค.
- ex ) @RequestMapping("hello-basic")
- @RequestMapping({"hello-basic", "hello-go")}
@RestController
public class MappingController {
private Logger log = LoggerFactory.getLogger(getClass());
@RequestMapping(value = "/hello-basic", method = RequestMethod.GET)
public String helloBasic() {
log.info("helloBasic");
return "ok";
}
}
๐ก HTTP ๋ฉ์๋
1๏ธโฃ ์์ ๊ฐ์ด @RequestMapping ์ method ์์ฑ์ผ๋ก HTTP ๋ฉ์๋๋ฅผ ์ง์ ํ ์ ์๋ค.
- ๋ง์ฝ HTTP ๋ฉ์๋๋ฅผ ์ง์ ํด์ฃผ์ง ์๋๋ค๋ฉด HTTP ๋ฉ์๋์ ๋ฌด๊ดํ๊ฒ ํธ์ถ๋ ์ ์๊ณ
- HTTP ๋ฉ์๋๋ฅผ ์ง์ ํด์ฃผ๋ฉด, ํด๋น HTTP ๋ฉ์๋๋ก๋ง ํธ์ถ์ด ๊ฐ๋ฅํ๋ค.
- ์ง์ ํ์ง ์์ HTTP ๋ฉ์๋๋ฅผ ํตํด ์์ฒญ์ด ๋ค์ด์ค๋ฉด 405๋ฅผ ๋ฐํํ๋ค.
2๏ธโฃ HTTP ๋ฉ์๋๋ฅผ ์ถ์ฝํ ์ ๋ ธํ ์ด์ ์ ์ฌ์ฉํ ์ ์๋ค.
- ์ด ๊ฒฝ์ฐ ์ข ๋ ์ง๊ด์ ์ผ๋ก ์ด๋ค ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋์ง ํ์ ํ ์ ์๋ค.
- ์ข
๋ฅ
- @GetMapping, @PostMapping, @PutMapping, @PatchMapping, @DeleteMapping
/**
* ํธ๋ฆฌํ ์ถ์ฝ ์ ๋
ธํ
์ด์
* @GetMapping
* @PostMapping
* @PutMapping
* @DeleteMapping
* @PatchMapping
*/
@GetMapping(value = "/mapping-get-v2")
public String mappingGetV2() {
log.info("mapping-get-v2");
return "ok";
}
๐กPathVariable (๊ฒฝ๋ก๋ณ์ ์ฌ์ฉ)
/**
* PathVariable(๊ฒฝ๋ก๋ณ์) ์ฌ์ฉ
* ๋ณ์๋ช
์ด ๊ฐ์ผ๋ฉด ์๋ต ๊ฐ๋ฅ
* @PathVariable("userId") String userId -> @PathVariable userId
* /mapping/userA
*
*/
@GetMapping("/mapping/{userId}")
public String mappingPath(@PathVariable("userId") String userId) {
log.info("mappingPath userId={}", userId);
return "ok";
}
- @PathVariable์ ์ฌ์ฉํ๋ฉด url์ ๋งค์นญ๋๋ ๋ถ๋ถ์ ํธ๋ฆฌํ๊ฒ ์กฐํํ ์ ์๋ค.
- ๋ง์ฝ @PathVariable ์ ์ด๋ฆ๊ณผ ํ๋ผ๋ฏธํฐ์ ์ด๋ฆ์ด ๊ฐ๋ค๋ฉด ์๋ตํ ์ ์๋ค.
- @PathVariable๋ ๋ค์ค์ผ๋ก๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
**
* PathVariable ์ฌ์ฉ ๋ค์ค
*/
@GetMapping("/mapping/users/{userId}/orders/{orderId}")
public String mappingPath(@PathVariable String userId, @PathVariable Long
orderId) {
log.info("mappingPath userId={}, orderId={}", userId, orderId);
return "ok";
}
๐ก ํน์ ํ๋ผ๋ฏธํฐ ์กฐ๊ฑด ๋งคํ
- ์ ์ฌ์ฉํ์ง๋ ์๋๋ค.
/**
* ํ๋ผ๋ฏธํฐ๋ก ์ถ๊ฐ ๋งคํ
* params="mode",
* params="!mode"
* params="mode=debug"
* params="mode!=debug" (! = )
* params = {"mode=debug","data=good"}
*/
@GetMapping(value = "/mapping-param", params = "mode=debug")
public String mappingParam() {
log.info("mappingParam");
return "ok";
}
๐ก ํน์ ํค๋ ์กฐ๊ฑด ๋งคํ
/**
*ํน์ ํค๋๋ก ์ถ๊ฐ ๋งคํ
* headers="mode",
* headers="!mode"
* headers="mode=debug"
* headers="mode!=debug" (! = )
*/
@GetMapping(value = "/mapping-header", headers = "mode=debug")
public String mappingHeader() {
log.info("mappingHeader");
return "ok";
}
- HTTP ํค๋๋ฅผ ์ฌ์ฉํ๋ค.
๐ก ๋ฏธ๋์ด ํ์ ์กฐ๊ฑด ๋งคํ - HTTP ์์ฒญ Content-Type, consume
/**
* Content-Type ํค๋ ๊ธฐ๋ฐ ์ถ๊ฐ ๋งคํ Media Type
* consumes="application/json"
* consumes="!application/json"
* consumes="application/*"
* consumes="*\/*"
* MediaType.APPLICATION_JSON_VALUE
*/
@PostMapping(value = "/mapping-consume", consumes = "application/json")
public String mappingConsumes() {
log.info("mappingConsumes");
return "ok";
}
- HTTP ์์ฒญ์ Content-Type ํค๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ฏธ๋์ด ํ์ ์ผ๋ก ๋งคํํ๋ค.
- ๋ง์ง ์๋๋ค๋ฉด HTTP 415 ์ํ์ฝ๋๋ฅผ ๋ฐํํ๋ค.
๐ก ๋ฏธ๋์ด ํ์ ์กฐ๊ฑด ๋งคํ - HTTP ์์ฒญ Accept, produce
/**
* Accept ํค๋ ๊ธฐ๋ฐ Media Type * produces = "text/html"
* produces = "!text/html"
* produces = "text/*"
* produces = "*\/*"
*/
@PostMapping(value = "/mapping-produce", produces = "text/html")
public String mappingProduces() {
log.info("mappingProduces");
return "ok";
}
}
- HTTP ์์ฒญ Accept ํค๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ฏธ๋์ด ํ์ ์ผ๋ก ๋งคํํ๋ค.
- ๋ง์ง ์๋๋ค๋ฉด HTTP 406 ์ํ์ฝ๋๋ฅผ ๋ฐํํ๋ค.
<๊น์ํ - '์คํ๋ง MVC 1ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํต์ฌ ๊ธฐ์ '๋ฅผ ์ฐธ๊ณ ํ์์ต๋๋ค.>
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [spring] ์คํ๋ง MVC - HTTP ์์ฒญ ๋ฉ์ธ์ง (0) | 2023.03.16 |
|---|---|
| [spring] ์คํ๋ง MVC - HTTP ์์ฒญ ํ๋ผ๋ฏธํฐ (0) | 2023.03.15 |
| [spring] logging (0) | 2023.03.13 |
| [spring] spring MVC ๊ตฌ์กฐ ์ดํด - dispatcherServlet (0) | 2023.03.12 |
| [spring] Front Controller (3) (0) | 2023.03.09 |