반응형
POST METHOD
HTTP POST요청이 들어왔을 때 동작
CRUD | 멱등성 | 안정성 | PathVariable | QueryParameter | DataBody |
C | X | X | O | △ | O |
멱등성: 같은 요청에 대해 여러번 반복해도 결과가 달라지지 않는 성질
Query Parameter : 데이터 처리하기위해 넘겨주는 매개변수(URL 중 ? 이후 문자열), &로 변수를 구분
ex) /user?id=abcd123&pw=1234
※ Query Parameter가 △인 이유?
해당 방식으로 데이터 생성할 수 있으나 일부 웹 서버는 URI길이가 제한 때문에, Query Parameter가 부적절한 경우 有
Path Variable : 데이터를 처리하기 위해 URI경로에 변수를 넣어줌
ex) /user/123
GET VS POST
GET방식과는 다르게 POST같은 경우 DataBody부에 데이터를 담아서 요청이들어온다.
POST 방식 요청에 대한 예제
package com.example.hello.controller;
import com.example.hello.dto.PostRequestDto;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController //API컨트롤러
@RequestMapping("/api")
public class PostApiController {
// JSON : 데이터를 주고받을때 형식
/*
* [Type]
* Stirng : value
* Number : value
* Boolean: value
* Object : value ==> { } 형태로 묶임
* Array : value ==> [ ] 형태로 묶임
*
* [JSON 형태]
* {
* "phone_number" : "010-5412-1315",
* "age" : 10
* "isAgree" : false,
* "account" : {
* "eamil" : "whaone2@naver.com",
* "password" : "1234"
* }
* "user_list" : [
* {
* "account" : "abcd",
* "password": "1234"
* },
* {
* "account" : "aaaaa",
* "password": "12344"
* },
* {
* "account" : "asdsd",
* "password": "153"
* }
* ]
* }
*
* */
//1) requestbody를 직접받는 경우
@PostMapping("/post")
public void post(@RequestBody Map<String, Object> requestData){
//key값은 String, value값은 여러 타입이 있으므로 object
//POST의 경우 body부를 받기 때문에 requestparam이 아닌 requestbody 어노테이션활용
requestData.forEach((key, value) -> {
System.out.println("key : " + key);
System.out.println("value : " + value);
});
}
//2) dto객체로 데이터 받는경우
@PostMapping("/post1")
public void post1(@RequestBody PostRequestDto postRequestDto){
System.out.println(postRequestDto.toString());
}
}
package com.example.hello.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
public class PostRequestDto {
private String account;
private String email;
private String address;
private String password;
@JsonProperty("phone_number") //해당 @ : 요청된 바디의 key값의 이름을 서버쪽 key값의 이름과 매핑
private String phoneNumber;
@Override
public String toString() {
return "PostRequestDto{" +
"account='" + account + '\'' +
", email='" + email + '\'' +
", address='" + address + '\'' +
", password='" + password + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
1) RequestBody를 직접 받는 경우
2) DTO객체로 데이터 받는 경우
반응형
'웹개발 > Spring & SpringBoot' 카테고리의 다른 글
[Spring]의존성 주입(Dependency Injection, DI) - 미완 (0) | 2022.11.05 |
---|---|
REST API - (4) DELETE METHOD 예제 (0) | 2022.10.24 |
REST API - (3) PUT METHOD 예제 (0) | 2022.10.23 |
REST API - (1) GET METHOD 예제 (0) | 2022.10.23 |