728x90
04. POST API
어노테이션
- @RestController : Rest API 설정
- @RequestMapping : 리소스를 설정 (method로 구분 가능)
- @PostMapping : Post Resource 설정
- @RequestBody : Request Body 부분 JSON Data Parsing.
- @JsonProperty : json naming
- @JsonNaming : class json naming
JSON
string : value
number : value
boolean : value
object : value { }
array : value [ ]
{
"phone_number" : "010-1111-2222",
"age" : 10,
"isAgree" : false,
"account" : {
"email" : "lee@gmail.com",
"password" : "1234"
}
}
// User 조회 하는 경우
{
"user_list" : [ // array
{ // Object
"account" : "asdf",
"password" : "1234"
},
{
"account" : "qwer",
"password" : "5678",
},
{
"account" : "zxcv",
"password" : "0987"
}
]
}
기본 구성
- POST 방식일 땐 메서드 파라미터에 @RequestBody라고 붙인다.
- 요청을 보낼 때 바디에 데이터를 심었다고 해서.
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
@RequestMapping("/api")
public class PostAPIController {
@PostMapping("/post")
public void post(@RequestBody Map<String, Object> requestData){
requestData.forEach((key, value) -> {
System.out.println("key : " + key);
System.out.println("value : " + value);
});
// 인텔리제이가 위의 코드로 간결하게 바꿔줬다.
// requestData.entrySet().forEach(stringObjectEntry -> {
// System.out.println("key : " + stringObjectEntry.getKey());
// System.out.println("value : " + stringObjectEntry.getValue());
// });
}
}
- 결과


예제
- PostRequestDto.java
package com.example.post.dto;
public class PostRequestDto {
// 요청하는 해당 json의 키의 값과 매칭이 되어야 한다.
private String account;
private String email;
private String address;
private String password;
...
get, set 메서드들..
...
@Override
public String toString() {
return "PostRequestDto{" +
"account='" + account + '\'' +
", email='" + email + '\'' +
", address='" + address + '\'' +
", password='" + password + '\'' +
'}';
}
}
- PostRequestController.java
import com.example.post.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;
@RestController
@RequestMapping("/api")
public class PostAPIController {
@PostMapping("/post")
// post 방식일 땐 @RequestBody라고 붙인다. 요청을 보낼 때 바디에 데이터를 심었다고 해서.
public void post(@RequestBody PostRequestDto requestData){
System.out.println(requestData);
}
}
- 결과

snake_case와 camelCase를 혼용해서 사용할 때
DTO
private String phoneNumber; // phone_number
API
"phone_number" : "123-456-7890”
결과
- phoneNumber가 null로 나온다.
PostRequestDto{account='user01', email='lee@gmail.com', address='seoul', password='asdaf', **phoneNumber='null'}**
이유
- 자바에서의 변수 선언은 카멜 케이스이고 API의 선언은 스네이크 케이스로 되어 있다. Text 데이터가 Object Mapper라는 라이브러리를 통해서 Text data가 자동으로 Object로 변한다. 그 때 따로 이름을 지정하지 않으면 스네이크 케이스로 매칭되는 이름을 찾아가기 때문에 카멜케이스라서 못 찾는다. 그래서 매칭해주는 방법을 사용해야 한다.
방법
- 방법은 여러가지가 있지만, JsonProperty를 사용한다.
- 카멜케이스나 스네이크케이스가 아니더라도 특정 이름에 대한 매칭도 가능하다.
- phoneNumber는 jsono객체에서 phone_number라는 이름으로 가질꺼야라는 뜻.
@JsonProperty("phone_number")
private String phoneNumber; // phone_number
@JsonProperty("OTP")
private String OTP; // 카멜케이스도 스네이크케이스도 아니다.
- Talent API - json body
{
"phone_number" : "123-456-7890",
"OTP" : "6875 9038"
}
- 결과

'개발 공부 > Spring' 카테고리의 다른 글
[SpringBoot] REST API - DELETE (0) | 2022.02.16 |
---|---|
[SpringBoot] REST API - PUT (0) | 2022.02.16 |
[SpringBoot] REST API - GET (0) | 2022.02.16 |
[SpringBoot] REST API & IntelliJ 프로젝트생성 (0) | 2022.02.16 |
[SpringBoot] 스프링 부트란 (0) | 2022.02.14 |