JSON / POST API


JSON


  • JSON : JavaScript Object Notation

  • λ„€νŠΈμ›Œν¬λ₯Ό 톡해 데이터λ₯Ό μ£Όκ³  λ°›λŠ” 데이터 ν˜•μ‹

  • key(name) - value ν•œ 쌍

      {
          "key : name"
      }
    
  • string / number / boolean / objecet : { } / array : [ ]

  • ex)

      {
          //string
          "phone_number" : "010-1234-5678",
    
          //number
          "age" : 10,
    
          //boolean
          "isAgree" : false,
    
          //object
          "account" : {
              "email" : "1234@gmail.com",
              "password" : "abcd1234"
          }
      }
    
    
      //user 쑰회 ν•˜λŠ” 경우
      //array
      {
          "user_list" : [
              {
                  "account" : "abcd",
                  "password: "1234" 
              },
              {
                  "account" : "aabb",
                  "password: "1212" 
              },
    
          ]
      }
    



POST API






package com.example.post_api;

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){
    // post둜 λ“€μ–΄μ˜€λŠ” json 데이터λ₯Ό λ§€ν•‘ν•˜κΈ° μœ„ν•΄μ„œλŠ” @RequestBody κΌ­ μ μ–΄μ€˜μ•Όν•¨!

        requestData.forEach((key, value) -> {
            System.out.println("key : " + key);
            System.out.println("value : " + value);

        });
    }
}


μ½”λ“œλ₯Ό μž‘μ„±ν•˜κ³  데이터λ₯Ό μ „μ†‘ν•˜λ©΄


μœ„μ™€ 같이 λ‚˜μ˜€λŠ” κ±Έ λ³Ό 수 μžˆλ‹€




  • PostRequestDto.java

      package com.example.post_api;
    
      public class PostRequestDto {
    
          private String account;
          private String email;
          private String password;
          //  μš”μ²­ν•˜λŠ” json의 킀에 ν•΄λ‹Ήλ˜λŠ” 값이 λ§€μΉ­λ˜μ–΄μ•Ό 함
    
          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 getPassword() {
              return password;
          }
    
          public void setPassword(String password) {
              this.password = password;
          }
    
          @Override
          public String toString() {
              return "PostRequestDto{" +
                      "account='" + account + '\'' +
                      ", email='" + email + '\'' +
                      ", password='" + password + '\'' +
                      '}';
          }
      }
    
  • PostApiController.java

      package com.example.post_api;
    
      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")
       
          public void post(@RequestBody PostRequestDto requestData){
    
              System.out.println(requestData);
    
          }
      }
    



    μœ„μ™€ 같은 방식을 μ΄μš©ν•΄

      requestData.getAccount();
      requestData.getEmail();
    

    와 같이 좜λ ₯ν•˜λ©΄ νŠΉμ • λ³€μˆ˜μ— μžˆλŠ” 데이터λ₯Ό κ°€μ Έμ˜¬ 수 μžˆλ‹€




@JsonProperty()


  • JSON의 ν‚€μ˜ 이름과 μžλ°” νŒŒμΌμ—μ„œμ˜ λ³€μˆ˜λͺ…이 λ‹€λ₯Ό 땐? -> @JsonProperty()

      import com.fasterxml.jackson.annotation.JsonProperty;
    
      @JsonProperty("phone_number")
      private String phoneNumber;
    

    -> νŠΉμ • 이름에 λŒ€ν•œ 맀핑 κ°€λŠ₯

Categories:

Spring