スプリングスタートPOST API

8457 ワード


  • JSON


  • 韓文詩

  • https://milenote.tistory.com/37?category=963202
  • 1、POST項目


    01. Spring Initalizr

  • https://start.spring.io/

  • 02.プロジェクト設定

  • resources/application.properties, server.port = 8090
  • Package : controller
  • Class : PostApiController
  • 03.プロジェクト作成

  • @RequestBody
  • クライアントから送信JSON形式のHTTP BodyコンテンツをJavaオブジェクトに変換する方法
  • @RequestBody受信データはSpring管理のMessageConverterの1つであるMappingJackson 2 HttpMessageConverterによりJavaオブジェクト
  • に変換される.

    -1. post()

  • PostApiController.java
  • package com.example.post.controller;
    
    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);
            });
    
        }
    }



    -2.dtoの作成

  • オブジェクトは
  • を使用しやすい.
  • Package : dto
  • Class : PostRequestDto
  • PostRequestDto.java
  • package com.example.post.dto;
    
    public class PostRequestDto {
        private String account;
        private String email;
        private String address;
        private String password;
    
        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;
        }
    
        @Override
        public String toString() {
            return "PostRequestDto{" +
                    "account='" + account + '\'' +
                    ", email='" + email + '\'' +
                    ", address='" + address + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }



    -3.Object Wrapper(JavaがCamelエンクロージャであり、Json転送がSnakeエンクロージャである場合)

  • JavaがCamelエンクロージャ、Json転送がSnakeエンクロージャの場合
  • PostRequestDto.java
  • package com.example.post.dto;
    
    public class PostRequestDto {
        private String account;
        private String email;
        private String address;
        private String password;
        private String phoneNumber; // phone_number
    
        public String getPhoneNumber() {
            return phoneNumber;
        }
    
        public void setPhoneNumber(String phoneNumber) {
            this.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;
        }
    
        @Override
        public String toString() {
            return "PostRequestDto{" +
                    "account='" + account + '\'' +
                    ", email='" + email + '\'' +
                    ", address='" + address + '\'' +
                    ", password='" + password + '\'' +
                    ", phoneNumber='" + phoneNumber + '\'' +
                    '}';
        }
    }

  • ソリューション
  • @JsonProperty(鍵が送信されました)
  • オブジェクトのJSONを変換するときに鍵の名前を開発者に必要な名前に設定する方法
  • .
  • Jacksonライブラリ
  • をダウンロードするには
  • PostRequestDto.java
  • package com.example.post.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")
        private String phoneNumber; // phone_number
    
        public String getPhoneNumber() {
            return phoneNumber;
        }
    
        public void setPhoneNumber(String phoneNumber) {
            this.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;
        }
    
        @Override
        public String toString() {
            return "PostRequestDto{" +
                    "account='" + account + '\'' +
                    ", email='" + email + '\'' +
                    ", address='" + address + '\'' +
                    ", password='" + password + '\'' +
                    ", phoneNumber='" + phoneNumber + '\'' +
                    '}';
        }
    }


  • SnakeでもCamelキャビネットでもないので@JsonProperty宣言を使用してください
  • OTPのように略語を使う場合





  • 3.構文整理テーブル(get,post)