Spring Boot | Spring MVC의 간단한 사용법 | 요청 바디(@RequestBody)값 얻기
코드 작성
src/main/java/sample/springboot/web/HelloController.java
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 void getMethod(@RequestBody String body) {
System.out.println("body=" + body);
}
}
실행 결과
curl으로 테스트하기
$ curl http://localhost:8080/hello -X POST -d "Request Body"
서버 콘솔 출력
body=Request Body
설명
@RequestBody
에서 요청 바디를 얻을 수있다.