simDev1234
심플하고 차분하게
simDev1234
전체 방문자
오늘
어제
  • 분류 전체보기
    • Computer Science
      • Basic Math
      • Data Structure
      • Algorithm
      • Database
      • OS
    • Language
      • Java
      • Kotlin
      • SQL
    • Framework
      • Spring
      • Orm&Mapper
      • 프로젝트로 스프링 이해하기
      • 스프링 라이브러리
    • Infra
      • Cloud
      • Docker
      • Redis
      • AWS, Azure
      • Device
    • Etc
      • CleanCoding
    • Git,Github

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 자바프로그래밍
  • 자바메모리구조
  • 자바프로그램
  • scanner #next() #nextLine()
  • null
  • JVM메모리구조
  • 스프링
  • 참조변수
  • 컨트롤러
  • 자바
  • 참조타입
  • 404
  • controllerTest

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
simDev1234

심플하고 차분하게

[HTTP] @RequestParam vs @RequestBody
Framework/Spring

[HTTP] @RequestParam vs @RequestBody

2022. 10. 19. 16:04

|  개요

- @RequestParam, @RequestBody

- @RequestParam은 URI를 통해서 넘겨 받는 값을 말하고,

- @RequestBody는 Http body에 데이터를 묶어서 받는 값을 말한다.

- 일반적으로 RequestParameter는 ?key=value와 같이 단일 데이터를 넘겨 받고,

  RequestBody는 x-www-form-urlencoded나 Json과 같이 특정 데이터 포맷으로 묶여서 담겨지는 경우가 많다.

- Post 방식으로 넘겨 받는 URI 모습

  *body 값으로 다양한 데이터 포맷이 존재하는 걸 볼 수 있다. 

|  실험

- POST 방식으로 Body에 담겨 넘어오는 데이터의 타입을 크게 두 가지로 분류해보았다. (물론 데이터 타입은 더 다양하다)

  1) x-www-form-urlencoded (form 디폴트 타입)

  2) json 타입

- Annotation, @RequestParam, @RequestBody에 따라

  두 가지 타입의 데이터를 어떻게 읽어오는지 아래의 메소드들을 사용해서 확인해보았다.

// 아래 각 케이스에 대하여 x-www-form / json 타입의 요청을 전달한다.

// case 1 : 단일 데이터 + @RequestParam
@PostMapping("/register")
public void register(@RequestParam String id){
    System.out.println(id); 
}

// case 2 : 단일 데이터 + @RequestBody
@PostMapping("/register2")
public void register2(@RequestBody String id){
    System.out.println(id); 
}

// case 3 : 객체 데이터 + @RequestParam
@PostMapping("/register3")
public void register3(@RequestParam UserRegister userRegister){
    System.out.println(userRegister); //
}

// case 4 : 객체 데이터 + @RequestBody
@PostMapping("/register4")
public void register4(@RequestBody UserRegister userRegister){
    System.out.println(userRegister); //
}

----- ++ 추가적으로 Annotion 미사용시 케이스도 확인

// case 5 : 단일 데이터 + Annotation 없음
@PostMapping("/register5")
public void register5(String id){
    System.out.println(id); //
}

// case 6 : 객체 데이터 + Annotation 없음
@PostMapping("/register6")
public void register6(UserRegister userRegister){
    System.out.println(userRegister); //
}

 

|  결과

  x-www-form-urlencoded json
단일 객체 단일 객체
요청 ) id=test1234 id=test1234&pwd=1234 {
    "id":"test1234"
}
{
     "id": "test1234",
     "pwd":"1234"
} 
@RequestParam test1234 400 에러** 400 에러* 400에러****
@RequestBody id=test1234 415 에러*** {
    "id":"test1234"
}
UserRegister(id=test1234, pwd=null)
Annotation 없음 test1234 com.example.demo.dto.UserRegister@498727e7 null com.example.demo.dto.UserRegister@498727e7

*400 : Required request parameter 'id' for method parameter type String is not present

**400 : Required request parameter 'userRegister' for method parameter type UserRegister is not present

***415 : Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

****400 : Required request parameter 'userRegister' for method parameter type UserRegister is not present

 

|  정리

- @RequestParam은 x-www-form-urlencoded 타입의 단일 변수 값을 읽어올 수 있다.

- @RequestBody는 json 타입의 단일 또는 객체 값을 읽어올 수 있다.

  x-www-form-urlencoded json
단일 객체 단일 객체
사용방식
@RequestParam 또는
Annotaion 미사용
Annotation 미사용 @RequestBody @RequestBody 또는
Annotaion 미사용

 

'Framework > Spring' 카테고리의 다른 글

[스프링] Build.Gradle & application.yml 관련 메모  (0) 2022.10.28
[스프링] @AutoWired 동작 원리 및 DI injection 관련 설명 모음  (0) 2022.10.20
[HTTP] User IP와 Agent(Device) 정보 가져오기  (0) 2022.10.15
[Validation] 데이터 검증, 비즈니스 로직 검증  (1) 2022.09.21
[스프링] Entity 객체를 생성 : 영속성의 개념 + 자동 Auditing  (0) 2022.09.15
    'Framework/Spring' 카테고리의 다른 글
    • [스프링] Build.Gradle & application.yml 관련 메모
    • [스프링] @AutoWired 동작 원리 및 DI injection 관련 설명 모음
    • [HTTP] User IP와 Agent(Device) 정보 가져오기
    • [Validation] 데이터 검증, 비즈니스 로직 검증
    simDev1234
    simDev1234
    TIL용 블로그. * 저작권 이슈가 있는 부분이 있다면 댓글 부탁드립니다.

    티스토리툴바