@GetMapping("string")
// @RequestMapping(value = "string", method = RequestMethod.GET)
// ๋ ์ฌ์ฉ ๊ฐ๋ฅ!
@ResponseBody
public String helloString() {
return "hello_string";
}
@GetMapping("json")
@ResponseBody
public Hello helloJson() {
Hello hello = new Hello();
hello.setName("example1");
hello.setEmail("example1@naver.com");
hello.setPassword("1234");
return hello;
}
@GetMapping("screen")
public String helloScreen() {
return "screen";
}
parameter ๋ฐฉ์
?{key}={value}
ex) localhost:8080/member/?id=1
path variable ๋ฐฉ์
url์ ํตํด ์์์ ๊ตฌ์กฐ๋ฅผ ๋ช ํํ๊ฒ ํํํ ์ ์๊ธฐ ๋๋ฌธ์ ๋ RESTful API ๋์์ธ์ ์ ํฉ
ex) localhost:8080/member/1
@GetMapping("param")
public String helloScreenModelParam(@RequestParam(value = "name") String inputName, Model model) {
// ํ๋ฉด์ data๋ฅผ ๋๊ธฐ๊ณ ์ถ์ ๋์๋ model๊ฐ์ฒด ์ฌ์ฉ
// model์ key:valueํ์์ผ๋ก ์ ๋ฌ
model.addAttribute("myData", inputName);
return "screen";
}
=> ex) http://localhost:8080/param/?name=example1๋ก ํธ์ถ
@GetMapping("pathvariable/{id}")
public String helloScreenModelPath(@PathVariable int id, Model model) {
model.addAttribute("myData", id);
return "screen";
}
=> ex) http://localhost:8080/pathvariable/1๋ก ํธ์ถ
Content-Type: application/x-www-form-urlencoded Content-Type: multipart/form-data @PostMapping("/form-post-handle1")
@ResponseBody
public String formPostHandle1(@RequestParam(value = "name") String name,
@RequestParam(value = "email") String email,
@RequestParam(value = "password") String password) {
return "์ ์์ฒ๋ฆฌ";
}
@PostMapping("/form-post-handle2")
@ResponseBody
// Data binding
// Spring์์ Hello ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์๋ mappingํด์ ์์ฑ
// x-www-url ์ธ์ฝ๋ฉ ํ์์ ๊ฒฝ์ฐ ์ฌ์ฉ
public String formPostHandle2(Hello hello) {
System.out.println(hello);
return "์ ์์ฒ๋ฆฌ";
}
Content-Type: application/json @PostMapping("/json-post-handle1")
@ResponseBody
public String jsonPostHandle1(@RequestBody Map<String, String> body) {
Hello hello = new Hello();
hello.setName((body.get("name")));
hello.setEmail((body.get("email")));
hello.setPassword((body.get("password")));
return "ok";
}
@PostMapping("/json-post-handle2")
@ResponseBody
public String jsonPostHandle2(@RequestBody JsonNode body) {
Hello hello = new Hello();
hello.setName((body.get("name").asText()));
hello.setEmail((body.get("email").asText()));
hello.setPassword((body.get("password").asText()));
return "ok";
}
}
@PostMapping("/json-post-handle3")
@ResponseBody
public String jsonPostHandle3(@RequestBody Hello hello) {
System.out.println(hello);
System.out.println(hello.getName());
System.out.println(hello.getEmail());
System.out.println(hello.getPassword());
return "ok";
}