REST API

(REST API) 요청값을 제한해서 받기

Accept 2022. 3. 12. 18:11

Event.java

	@Id @GeneratedValue
	private Integer id;
	private String name;
	private String description;
	private LocalDateTime beginEnrollmentDateTime; // 등록 시작 일시
	private LocalDateTime closeEnrollmentDateTime; // 등록 종료 일시
	private LocalDateTime beginEventDateTime; // 이벤트 시작 일시
	private LocalDateTime endEventDateTime;  // 이벤트 종료 일시
	private String location; // (optional) 있으면 로컬, 없으면 온라인
	private int basePrice; // (optional) 등록비
	private int maxPrice; // (optional) != 0 선착순, == 0 무제한
	private int limitOfEnrollment; // 등록 제한 횟수
	private boolean offline;
	private boolean free; // 유료, 무료
	@Enumerated(EnumType.STRING) // Enum 순서가 변경될 경우, 로직이 깨질 수 있어서 String으로 사용 권장
	private EventStatus eventStatus;

EventDto.java

	private String name;
	private String description;
	private LocalDateTime beginEnrollmentDateTime; // 등록 시작 일시
	private LocalDateTime closeEnrollmentDateTime; // 등록 종료 일시
	private LocalDateTime beginEventDateTime; // 이벤트 시작 일시
	private LocalDateTime endEventDateTime;  // 이벤트 종료 일시
	private String location; // (optional) 있으면 로컬, 없으면 온라인
	private int basePrice; // (optional) 등록비
	private int maxPrice; // (optional) != 0 선착순, == 0 무제한
	private int limitOfEnrollment; // 등록 제한 횟수

 

요청값을 제한해서 받고 싶을 경우, 요청값에 맞는 DTO를 만들어서 매개인자에 담아준다.

그만큼 DTO 클래스가 많아지겠지만, 절대로 받아서는 안되는 파라미터 값을 방지하기 위해 DTO를 적극 활용해야겠다.

 

'REST API' 카테고리의 다른 글

HTTP METHOD에서 PUT 과 PATCH의 차이점  (0) 2022.04.17
Content-Type 과 Accept Header의 차이점  (0) 2022.04.17
(REST API) 사용/개발 조건  (0) 2022.03.12