💡 학습 목표
JSON 이해 POST 주소 맵핑, @RequestBody를 Map 구조로 설정
JSON 형식을 만들고 POST 방식으로 데이터 보내기
DTO 만들어서 사용해보기
@JsonProperty 사용해 보기
스네이크 케이스와 카멜케이스 구분
💡 JSON 데이터 타입 확인
- 문자열 ("name": "John")
- 숫자 ("age": 30)
- 불리언 ("isStudent": false)
- 객체 ("address": { "city": "New York", "zipCode": "10001" })
- 배열 ("hobbies": ["reading", "traveling", "swimming"])
- null ("middleName": null)
{
"name": "John",
"age": 30,
"isStudent": false,
"address": {
"city": "New York",
"zipCode": "10001"
},
"hobbies": ["reading", "traveling", "swimming"],
"middleName": null
}
package com.tenco.demo_v2.controller;
import java.util.Map;
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 com.tenco.demo_v2.dto.UserDTO;
@RestController // IOC 대상
@RequestMapping("/post")
public class PostApiController {
// 주소 설계
// http://localhost:8080/post/demo1 METHOD - Post
// 테스트 데이터
//
@PostMapping("/demo1")
// 사용자가 던진 데이터를 바인딩 처리 -> HTTP 메시지 바디 영역
public String demo1(@RequestBody Map<String, Object> reqData) {
// POST --> 리소스 취득,생성 --> DB 접근기술 --> 영구히 데이터를 저장한다.
StringBuffer sb = new StringBuffer();
reqData.entrySet().forEach((entry) -> {
sb.append(entry.getKey() + "=" + entry.getValue());
});
// 메세지 컨버터가 동작 (리턴 타입 String )
return sb.toString();
}
@PostMapping("/demo2")
public UserDTO demo2(@RequestBody UserDTO userDTO) {
return userDTO;
}
}
'Spring boot' 카테고리의 다른 글
JPA 와 하이버네이트란? (0) | 2024.09.30 |
---|---|
스프링 부트 DB 접근 기술 ORM (0) | 2024.09.30 |
GET 방식과 URL 주소 설계 (0) | 2024.09.28 |
스프링 부트 간단한 요청과 응답 동작 방식을 알아보자. (0) | 2024.09.27 |
스프링 부트의 웹 애플리케이션 구조 어떻게 만들어져 있을까? (0) | 2024.09.27 |