본문 바로가기
Spring Framework/Spring

@RequestBody 여러개 사용방법

by wakestand 2022. 2. 8.
반응형
@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody String str1, @RequestBody String str2) {}

 

위 코드와 같이 @RequestBody를 2개 사용해서

여러 변수를 넘겨주려는 경우

 

@RequestBody는 여러개 사용할 수 없기 때문에

이런 방법으로 파라미터를 넘겨줄 수는 없고

 

{
    "str1": "test one",
    "str2": "two test"
}

 

이렇게 2개의 변수를 넘겨야 한다고 치면

 

let requestModel = {
    str1 : "test one",
    str2 : "two test"
}

 

일허게 변수를 선언한 뒤

변수 안에 여러 파라미터로 사용할 값을 넣어준 뒤

 

@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody ModelTest model) {}
public class ModelTest {
  private String str1;
  private String str2;
}

 

이런 식으로 모델로 사용할 클래스를 만들어서

여러 파라미터를 받아준 뒤

Getter를 이용해서 끌어 사용하면 된다

반응형

댓글