Light Blue Pointer
본문 바로가기
Developing/TIL(Develop)

클라이언트에서 데이터를 받아오는 방법

by Greedy 2023. 12. 6.

클라이언트에서 데이터를 받아오는 방법

  1. Path Variable (GET) 방식 → Primitive 타입으로 받는다
  2. Request Param (GET)방식 → Primitive 타입으로 받는다
  3. Request Param (POST) 방식
  4. Model Attribute 방식 → QueryString을 객체로 받는다
  5. Request Body 형식 → JSON을 객체로 받는다

1. Path Variable (GET)방식 → Primitive 타입으로 받는다

 

html에서 /hello/request/star/${name}/age/${age} 데이터를 전송

// [Request sample]
// GET <http://localhost:8080/hello/request/star/Margot/age/33>
@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 경로에서 데이터를 받고자 하는 위치의 경로에 {data} 중괄호를 사용

: /star/{name}/age/{age}

해당 요청 메서드 파라미터에 @PathVariable 애너테이션과 함께 {name} 중괄호에 선언한 변수명과 변수타입을 선언해서 해당 경로의 데이터를 받아옴

: (@PathVariable String name, @PathVariable int age)

 

2. Request Param (GET)방식 → Primitive 타입,Query String 형식,Header부분으로 받는다

// [Request sample]
// GET <http://localhost:8080/hello/request/form/param?name=Margot&age=33>
@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);
}

Query String 형식(?Key=Value&Key=Value)으로 데이터를 전송함

Q.Header부분으로 받는게 맞나요?

 

3. Request Param (POST) 방식→ Primitive 타입, Query String 형식, Body부분으로 받는다

요청이 Body 부분으로 넘어감

// [Request sample]
// POST <http://localhost:8080/hello/request/form/param>
// Header
//  Content type: application/x-www-form-urlencoded
// Body
//  name=Margot&age=33
@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);
}

Request Param은 생략이 가능하다

 

4. Model Attribute 방식 → 객체 타입, QueryString형식, Body부분으로 받는다

Body 부분에 들어온 QueryString 방식의 데이터를 객체에 매핑해서 가지고 옴

// [Request sample]
// POST <http://localhost:8080/hello/request/form/model>
// Header
//  Content type: application/x-www-form-urlencoded
// Body
//  name=Margot&age=33@PostMapping("/form/model")
@ResponseBody
public String helloRequestBodyForm(@ModelAttribute Star star) {
    return String.format("Hello, @ModelAttribute.<br> (name = %s, age = %d) ", star.name, star.age);
}

@ModelAttribute을 사용할때는

클라이언트에서 전송하는 Query String의 key값이 객체의 field명과 일치해야 한다

객체를 매개변수로 사용할때는 Getter,Setter 혹은 오버로딩된 생성자가 꼭 있어야 한다

 

5. Request Body 형식 → 객체타입, Json형식 ,Body부분으로 받는다

// [Request sample]
// POST <http://localhost:8080/hello/request/form/json>
// Header
//  Content type: application/json
// Body
//  {"name":"Margot","age":"33"}@PostMapping("/form/json")
@ResponseBody
public String helloPostRequestJson(@RequestBody Star star) {
    return String.format("Hello, @RequestBody.<br> (name = %s, age = %d) ", star.name, star.age);
}

객체를 매개변수로 사용할때는 Getter,Setter 혹은 오버로딩된 생성자가 꼭 있어야 한다

 

 

'Developing > TIL(Develop)' 카테고리의 다른 글

📖Entity에 복합키로 id구성하는 방법 @IdClass, @EmbeddedId  (0) 2024.03.15
📖Hibernate/JPA의 id 생성 전략들  (0) 2024.01.17
인증과 인가  (2) 2023.11.20
Spring Bean  (0) 2023.11.20
JPA in Spring Boot  (0) 2023.11.15