Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

햄발

Spring Boot Blog 프로젝트 만들기(JPA) - POST 방식에 이해 및 실습 본문

Spring boot

Spring Boot Blog 프로젝트 만들기(JPA) - POST 방식에 이해 및 실습

햄발자 2024. 9. 28. 00:16

 

 

학습 목표

1. JSON 이해
2. POST 주소 맵핑, @RequestBody를 Map 구조로 설정
3. JSON 형식을 만들고 POST 방식으로 데이터 보내기
4. DTO 만들어서 사용해보기
5. @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
}

 

PostApiController.java

package com.tenco.demo_v1.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;


@RestController // IoC 대상 
@RequestMapping("/post")
public class PostApiController {

    // 주소 설계 
    // http://localhost:8080/post/demo1    METHOD - Post 
    // 테스트 데이터  - { "name" : "둘리", "age": 11 }
    // return String 
    @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();
    }
}

 

DemoV1Application.java

package com.tenco.demo_v1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoV1Application {

	public static void main(String[] args) {
		SpringApplication.run(DemoV1Application.class, args);
	}

}

 


UserDTO.java

package com.tenco.demo_v1.dto;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
     * 주소 설계
     * http://localhost:8080/post/demo2 Post 방식
     * http://localhost:8080/post/demo2
     * @param 본문 - {"name" : "둘리", "age" : 11, "phone_number" : "010-1234-5678"}
     * @param JSON
     * [Post 요청에서 본문이 있다. RequestBody - Object 로 파싱]
     * 테스트 - post man 활용
     * DTO 방식으로 데이터 내려 보기
     * @RequestBody 생략 가능
     */

@RestController // IoC 대상 
@RequestMapping("/post")
public class UserDTO {
    
    @PostMapping("/demo2")
    public String postTest(@RequestBody Map<String, Object> reqData) {
        StringBuffer sb = new StringBuffer();
        reqData.entrySet().forEach((entry)-> {
            sb.append(entry.getKey() + " @=@ " + entry.getValue());
        });
        
        return sb.toString();
    }
    

}

 

DemoV1Application.java

package com.tenco.demo_v1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoV1Application {

	public static void main(String[] args) {
		SpringApplication.run(DemoV1Application.class, args);
	}

}