미소를뿌리는감자의 코딩
[Spring] HTTP 데이터를 객체로 바꾸는 법 본문
728x90
1. html을 반환하는 방법
@GetMapping("/form/html")
public String helloForm() {
return "hello-request-form";
}
2. pass variable 방식
// [Request sample] // pass browser 방식
// GET http://localhost:8080/hello/request/star/Robbie/age/95
@GetMapping("/star/{name}/age/{age}")
@ResponseBody
public String helloRequestPath(@PathVariable String name, @PathVariable int age)
{
return String.format("Hello, @PathVariable.<br> name = %s, age = %d", name, age);
}
url 내에 정보가 들어간다.
@PathVariable로 url에서 가지고 옴을 명시
3. request param - get 방식
// [Request sample] request param 방식
// GET http://localhost:8080/hello/request/form/param?name=Robbie&age=95
@GetMapping("/form/param")
@ResponseBody
public String helloGetRequestParam(@RequestParam(required = false) String name, int age) {
return String.format("Hello, @RequestParam.<br> name = %s, age = %d", name, age);
}
required = false를 해주면, 어떤 값이 들어오지 않으면, null을 반환해 준다.
4. request param - post 방식
// [Request sample] post 방식으로 넘어오는 것을 request param으로 받는 방법
// POST http://localhost:8080/hello/request/form/param
// Header
// Content type: application/x-www-form-urlencoded
// Body
// name=Robbie&age=95
@PostMapping("/form/param")
@ResponseBody
public String helloPostRequestParam(@RequestParam String name, @RequestParam int age) {
return String.format("Hello, @RequestParam.<br> name = %s, age = %d", name, age);
}
post 방식으로 넘어오는 것은 body 형태로 넘어온다.
개발자 도구에서 이를 확인 가능하다.
728x90
'강의수강 > [Spring]' 카테고리의 다른 글
[Spring] Entity & JPA & ORM와 IoC & DI (0) | 2024.03.04 |
---|---|
[Spring] compiler warning: local variable is redundant (0) | 2024.03.02 |
[Spring] Jackson ( Jason to Object, Object to Jason ) (1) | 2024.02.29 |
[Spring] Response Json (0) | 2024.02.29 |
[Spring] 정적, 동적 html 처리 (0) | 2024.02.29 |