.png)

// [Request sample]
// POST http://localhost:8080/hello/request/form/model
// Header
// Content type: application/x-www-form-urlencoded
// Body
// name=Robbie&age=95
@PostMapping("/form/model")
@ResponseBody
public String helloRequestBodyForm(@ModelAttribute Star star) {
// body에 들어있는 데이터를 객체로 래핑해서 가져올때
// HTTP Body에 name=Robbie&age=95 형태로 담겨져서 서버로 전달
return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age);
}
// [Request sample]
// GET http://localhost:8080/hello/request/form/param/model?name=Robbie&age=95
@GetMapping("/form/param/model")
@ResponseBody
public String helloRequestParam(Star star) {
// setter나 오버로딩된 생성자가 있어야지 스프링내부에서 받은값을 넣어줄 수 있다
// 스프링이 자체적으로 심플밸류타입(int등)이면 RequestParam이 생략되었다고 인식하고 그렇지 않으면 ModelAttribute가 생략되었다고 인식함
return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age);
}
// [Request sample]
// POST http://localhost:8080/hello/request/form/json
// Header
// Content type: application/json
// Body
// {"name":"Robbie","age":"95"}
@PostMapping("/form/json")
@ResponseBody
public String helloPostRequestJson(@RequestBody Star star) {
return String.format("Hello, @RequestBody.<br> (name = %s, age = %d) ", star.name, star.age);
}
package com.sparta.springmvc.request;
public class Star {
String name;//우클릭 -> refactor-> rename 같은이름 전부 수정가능
int age;
public Star(String name, int age) {
this.name = name;
this.age = age;
}
}