Spring Boot | Spring Boot 이란? | 응답과 요청 매핑
응답과 요청 매핑(Mapping Requests and Responses)
src/main/java/sample/springboot/web/Hoge.java
package sample.springboot.web;
public class Hoge {
public int id;
public String value;
@Override
public String toString() {
return "Hoge [id=" + id + ", value=" + value + "]";
}
}
src/main/java/sample/springboot/web/HelloController.java
package sample.springboot.web;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method=RequestMethod.POST)
public Hoge hello(@RequestBody Hoge param) {
System.out.println(param);
Hoge hoge = new Hoge();
hoge.id = 20;
hoge.value = "Response";
return hoge;
}
}
어플리케이션 실행
curl으로 테스트하기
$ curl -H "Content-type: application/json" -X POST -d '{"id": 10, "value": "Request"}' http://localhost:8080/hello
{"id":20,"value":"Response"}
서버 콘솔 출력
Hoge [id=10, value=Request]
설명
- 기본적으로 요청과 응답은 모두 JSON 의한 매핑이 되어 있다.
- 매핑은 Jackson으로 하고 있고 있다 (그래서 매핑 조정은 Jackson 어노테이션으로 가능하다).