아이앰포트(포트원) api를 스프링부트에서 사용해 예약결제를 구현하던 중 ScheduleData 클래스 안에 @SerializedName 어노테이션이 붙은 것을 봤다.
https://cherrypick.co.kr/gson-serializedname-annotation/
Gson은 직렬화와 역직렬화를 처리하기 위한 자바 라이브러리다.
객체를 json과 같이 데이터를 전송할 수 있는 포맷으로 변경하는 것.
json과 같은 형태를 객체로 변환하는 것
따라서 아이앰포트 api 안의 ScheduleData 클래스는 아이앰포트 서버로 요청을 보낼 때 전송할 객체를 Json의 형태로 직렬화 해주는 기능을 한다.
public class ScheduleData {
@SerializedName("customer_uid")
private String customer_uid;
@SerializedName("schedules")
private List<ScheduleEntry> schedules;
public ScheduleData(String customer_uid) {
this.customer_uid = customer_uid;
this.schedules = new ArrayList<ScheduleEntry>();
}
public void addSchedule(ScheduleEntry entry) {
schedules.add(entry);
}
public List<ScheduleEntry> getSchedules() {
return schedules;
}
}
@SerializedName 안에 있는 문자열을 Json의 키값으로 보내게 된다.
직렬화 한다고 다른 로직 없이 그냥 생성자를 통해 객체 생성 후 addSchedule 함수를 통해 스케쥴 값을 ScheduleData에 넣으면 끝!
이 클래스로 요청 보내면 직렬화 된 데이터가 보내질거다.