
@RestController
- @Controller๋ ๋ฐํ ๊ฐ์ด String์ด๋ฉด ๋ทฐ ์ด๋ฆ์ผ๋ก ์ธ์๋๋ค. ๊ทธ๋์ ๋ทฐ๋ฅผ ์ฐพ๊ณ ๋ทฐ๊ฐ ๋ ๋๋ง ใ ๋๋ค.
- @RestController๋ ๋ฐํ ๊ฐ์ผ๋ก ๋ทฐ๋ฅผ ์ฐพ๋ ๊ฒ์ด ์๋๋ผ ,HTTP ๋ฉ์์ง ๋ฐ๋์ ๋ฐ๋ก ์ ๋ ฅํ๋ค.
@RequestMapping("/hello-basic")
- /hello-basic : URL ํธ์ถ์ด ์ค๋ฉด ์ด ๋ฉ์๋๊ฐ ์คํ๋๋๋ก ๋งคํํ๋ค.
- ๋๋ถ๋ถ์ ์์ฑ์
๋ฐฐ์ด[]๋ก ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ ๋ค์ค ์ค์ ์ด ๊ฐ๋ฅํ๋ค.["/hello-basic", "/hello-go"]
์ฐธ๊ณ
์คํ๋ง ๋ถํธ 3.0 ์ด์
- ์คํ๋ง์ ๋ค์ URL ์์ฒญ๋ค์ ๊ฐ์ ์์ฒญ์ผ๋ก ๋งคํ
- ๋งคํ :
/hello-basic- URL ์์ฒญ :
/hello-basic, /hello-basic/
์คํ๋ง ๋ถํธ 3.0 ์ดํ
- ๋ง์ง๋ง์
/๋ฅผ ์ ์งํ๋ค.- ๋งคํ :
/hello-basic-> URL ์์ฒญ :/hello-basic- ๋งคํ :
/hello-basic/-> URL ์์ฒญ :/hello-basic/
@RequestMapping์ method ์์ฑ์ผ๋ก HTTP ๋ฉ์๋๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด HTTP ๋ฉ์๋์ ๋ฌด๊ดํ๊ฒ ํธ์ถ๋๋ค./**
* method ํน์ HTTP ๋ฉ์๋ ์์ฒญ๋ง ํ์ฉ
* GET, HEAD, POST, PUT, PATCH, DELETE
*/
@RequestMapping(value = "/mapping-get-v1", method = RequestMethod.GET)
public String mappingGetV1() {
log.info("mappingGetV1");
return "ok";
}
/**
* ํธ๋ฆฌํ ์ถ์ฝ ์ ๋
ธํ
์ด์
(์ฝ๋๋ณด๊ธฐ)
* @GetMapping
* @PostMapping
* @PutMapping
* @DeleteMapping
* @PatchMapping
*/
@GetMapping(value = "/mapping-get-v2")
public String mappingGetV2() {
log.info("mapping-get-v2");
return "ok";
}
@RequestMapping๊ณผ method๋ฅผ ์ง์ ํด์ ์ฌ์ฉํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. /**
* PathVariable ์ฌ์ฉ
* ๋ณ์๋ช
์ด ๊ฐ์ผ๋ฉด ์๋ต ๊ฐ๋ฅ
* @PathVariable("userId") String userId -> @PathVariable String userId
*/
@GetMapping("/mapping/{userId}")
public String mappingPath(@PathVariable("userId") String data) {
log.info("mappingPath userId={}", data);
return "ok";
}
/mapping/userA/users/1@RequestMapping์ 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";
}
/**
* 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";
}
์์
consumes = "text/plain"
consumes = {"text/plain" "application/*"}
consumes = MediaType.TEXT_PLAIN_VALUE
/**
* 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";
}
์์
produces = "text/plain"
produces = {"text/plain", "application/*"}
produces = MediaType.TEXT_PLAIN_VALUE
produces = "text/plain;charset=UTF-8"
- ํ์ ๊ด๋ฆฌ API(์์)
| API ๋ชฉ๋ก | HTTP METHOD | URL |
|---|---|---|
| ํ์ ๋ชฉ๋ก ์กฐํ | GET | /users |
| ํ์ ๋ฑ๋ก | POST | /users |
| ํ์ ์กฐํ | GET | /users/{userId} |
| ํ์ ์์ | PATCH | /users/{userId} |
| ํ์ ์ญ์ | DELETE | /users/{userId} |
@RestController
@RequestMapping("/mapping/users")
public class MappingClassController {
@GetMapping
public String user() {
return "get users";
}
@PostMapping
public String addUser() {
return "post user";
}
@GetMapping("/{userId}")
public String findUser(@PathVariable String userId) {
return "get userId = " + userId;
}
@PatchMapping("/{userId}")
public String updateUser(@PathVariable String userId) {
return "update userId = " + userId;
}
@DeleteMapping("/{userId}")
public String deleteUser(@PathVariable String userId) {
return "delete userId = " + userId;
}
}
์ด๋ ๊ฒ Spring์์ http ์์ฒญ์ ๋ฐ์์๋ ์ด๋ป๊ฒ request์์ฒญ์ ๋ฐ๋์ง ๊ทธ๋ฆฌ๊ณ request์ ์ด๋ ํ ์ ์ฝ์ ๋ถ์ฌํ๊ณ ์ ์ฝ์ด ๋ง์์ ๊ฒฝ์ฐ์๋ง ์์ฒญ์ ๋ฐ์๋ค์ผ์ ์๋์ง, ๊ธฐ๋ณธ์ ์ธ ํํ์ HTTP METHOD ์์ฒญ์ด ์ค๋ฉด ์๋ํ๋ API๋ฅผ ๊ฐ๋ณ๊ฒ ๋ง๋ค์ด ๋ณด์๋ค.