TIL | [Spring] Controller에서 Request

hyemin·2022년 4월 7일
0

Spring

목록 보기
6/7
post-thumbnail

앞서 Controller에서 Response 유형에 대해 정리했고 이번에는 Requet 유형에 대해 정리해보려 한다.

@PathValiable

HelloRequestController.java

@Controller
@RequestMapping("/hello/request")
public class HelloRequestController {
    // [Request sample]
	// GET http://localhost:8080/hello/request/star/yjs/age/51
    @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);
    }
}

Request URL에 담긴 name, age 변수 정보를 @PathValiable을 통해 가져옴

http://localhost:8080/hello/request /star/yjs/age/51

@RequsetParam

  • @RequsetParam은 생략이 가능하다

1. GET 요청

HelloRequestController.java

@Controller
@RequestMapping("/hello/request")
public class HelloRequestController {
    // [Request sample]
	// GET http://localhost:8080/hello/request/form/param?name=BTS&age=28
    @GetMapping("/form/param")
    @ResponseBody
    public String helloGetRequestParam(@RequestParam String name, @RequestParam int age) {
        return String.format("Hello, @RequestParam.<br> name = %s, age = %d", name, age);
    }
}

http://localhost:8080/hello/request/form/param
?name=yjs&age=51
GET 요청시 form으로 보내도 String 파라미터로 받아 URL에 정보가 담기게 된다.

2. POST 요청

HelloRequestController.java

@Controller
@RequestMapping("/hello/request")
public class HelloRequestController {
    // [Request sample]
    // POST http://localhost:8080/hello/request/form/param    
    @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);
    }
}

http://localhost:8080/hello/request/form/param
POST 요청시 form으로 보내면 GET 요청시 URL에 정보가 담겼던 것과 다르게 body에 Form Data로 정보가 들어오게 된다.

@ModelAttribute

  • 위의 @RequestParam의 POST 요청과 같은 방식이다. 다만, Star라는 객체에서 정보를 받아온다는 차이가 있다.
    만약, 받아올 정보가 name, age 외에 무수히 많다면 @RequestParam을 이용하는 것에 한계가 있을 것이다.

  • Star 객체 클래스에 @Setter 설정이 되어있어야 한다.

HelloRequestController.java

@Controller
@RequestMapping("/hello/request")
public class HelloRequestController {
    // [Request sample]
    // POST http://localhost:8080/hello/request/form/model
    @PostMapping("/form/model")
    @ResponseBody
    public String helloRequestBodyForm(@ModelAttribute Star star) {
        return String.format("Hello, @RequestBody.<br> (name = %s, age = %d) ", star.name, star.age);
    }
}

http://localhost:8080/hello/request/form/model

@RequestBody - JSON

  • 페이지는 그대로 있고 변경이 필요한 부분의 데이터만 받아와 변경해준다.

HelloRequestController.java

@Controller
@RequestMapping("/hello/request")
public class HelloRequestController {
	// [Request sample]
	// POST http://localhost:8080/hello/request/form/json
    @PostMapping("/form/json")
    @ResponseBody
    public String helloPostRequestJson(@RequestBody Star star) {
        return String.format("Hello, @RequestBody.<br> (name = %s, age = %d) ", star.name, star.age);
    }
}

위에 예시들과 달리 Content-Type이 application/json으로 되어있다.

http://localhost:8080/hello/request/form/json
POST 요청시 받아온 데이터도 JSON 형식의 데이터로 받아오게 된다.

요약

0개의 댓글