Post API / 패스트캠퍼스 챌린지 01일차

망고쥬스·2021년 11월 1일
0

PostApi

PostApiController.java

@RestController
@RequestMapping("/api")
public class PostApiController {
    @PostMapping("/post")
    public void post(@RequestBody PostRequestDto requestData){
            System.out.println(requestData);
            System.out.println(requestData.getAccount());
    }
}

Json에서 데이터를 받아와 (Post방식의 전송에서 @RequestBody를 통해 데이터를 통으로 들고올 수 있다) PostRequestDto에 적용되어진 객체를 통해 값을 저장.
즉 PostRequestDto 자료형의 requestData 객체에 값을 다 저장해준다.

PostRequestDto.java

@JsonProperty를 통해 Snake 방식과 Camel 방식을 혼용해서 사용이 가능하다.
Json방식에서는 소문자만 인식을 한다. 즉 OTP와 같이 대문자로만 이루어진 키들은 @JsonProperty를 사용하여 명시하거나, 소문자로만 작성해야한다. // 3
toString 자동완성 기능을 통해 손 쉽게 출력문을 작성하고 출력할 수 있다.

package com.example.hello.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
    @JsonProperty("OTP") //3
    private String OTP;

    public String getOTP() {
        return OTP;
    }

    public void setOTP(String OTP) {
        this.OTP = OTP;
    }

    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 + '\'' +
                ", OTP='" + OTP + '\'' +
                '}';
    }
}

#패스트캠퍼스 #패캠챌린지 #직장인인강 #직장인자기계발 #패스트캠퍼스후기 #한번에끝내는Java/Spring웹개발마스터초격차패키지Online

본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성되었습니다.

https://bit.ly/3FVdhDa

profile
#newbieDeveloper #since 2021.04.06

0개의 댓글