💻Development
-
API & Response Body 형태 구성Back-end/TIL 2022. 4. 13. 09:24
스타트업 백엔드 인턴으로 근무하여 배운 내용을 까먹지 않기 위해 정리하는 글입니다. # API 형태 구성 API 반환 값은 Json Object (Key, Value)로 구성하는 것이 좋다. 그래야 프론트에서도 (Key, Value)로 접근하기 편리하고, 확장성도 좋다. 응답 코드도 함께 반환해주는 것이 좋다. 응답 메시지는 변경될 가능성이 크기 때문에 프론트에서 처리할 때 응답 코드로 처리하는 것이 훨씬 편하다. 느낀점 파피메일 프로젝트를 할 때는 응답 메시지만 다르게 반환해서 프론트에서 구별해주셨는데 미안한 마음이 들었다. 다음 프로젝트 때는 응답 코드도 필수로 반환하도록 개선해야겠다. # Response Body 형태 응답 코드, 응답 메시지를 포함한 통일된 응답 형태로 반환하기 위해 Respons..
-
[SpringBoot] JPA Entity와 데이터베이스 간 Enum 매핑Back-end/TIL 2022. 4. 12. 08:58
스타트업 백엔드 인턴으로 근무하며 배운 내용을 까먹지 않도록 하기 위해 정리하는 글입니다. 회사에서 약관 관련 업무를 진행하며 약관 필드의 타입이 Enum으로 설정되어 있었고, 이를 계기로 Enum 저장 및 반환에 대해 찾아보았다. 📌 Enum이란 무엇인가 기존에 상수를 정의할 때 사용하던 final static String 방식을 개선하여 나온 것으로, Enum은 열거형이라고 불리며, 서로 연관된 상수들의 집합을 의미한다. 📌 Enum 타입의 Entity 필드를 데이터베이스에 저장하는 방법 방식1. @Enumerated(value = EnumType.{...}) ORDINAL - 해당 Enum의 index 값이 DB에 저장된다. 단점 Enum 클래스에 정의해둔 Enum 값들의 순서가 바뀔 경우 원하는 ..
-
[번외] 서비스 이용약관 처리 방식Back-end/TIL 2022. 4. 12. 08:24
인턴으로 근무하고 있는 회사에서 서비스 이용약관 조회 API를 만들며 이용약관을 처리하는 2가지 방식에 대해 알게 되었다. 방식1. HTML로 처리 이용약관은 법으로 지정되어 있으므로, 반드시 보여줘야 한다. 이때 HTML로 화면을 구성해서 DB 조회를 해오지 않고, 바로 불러오도록 구성한다. 이전 약관내용도 보여줘야 한다. 장점: DB 부하 방지 가능 단점: 해당 페이지를 관리하는 인력이 필요함 흔히 이렇게 처리가 되고, 추구되는 방향이다. 방식2. DB에 약관 내용 저장 약관 조회 페이지 클릭 시 DB로부터 약관 내용을 전달받는다. 단점: 요청이 많아지면 DB 부하가 발생할 수 있다. 개선: 이를 개선할 방법으로는 캐시에 저장하는 것이다. Spring의 경우 redis와의 연결성이 좋아 Service..
-
[JPA] 엔티티 매핑Back-end/JPA 2022. 4. 11. 22:39
JPA에서 가장 중요한 2가지는 '객체와 관계형 데이터베이스 매핑' & '영속성 컨텍스트' 이다. # 객체와 테이블 매핑 @Entity @Entity가 붙은 클래스는 JPA가 관리하는 엔티티라고 한다. JPA를 사용해 테이블과 매핑할 클래스에는 반드시 @Entity를 지정해줘야 한다. @Entity가 붙은 클래스는 기본 생성자가 필수이다. (public or protected 생성자) final 클래스, enum, interface, inner 클래스에는 사용할 수 없다. 클래스의 필드에 final을 사용할 수 없다. Q. @Entity가 붙은 클래스에 기본 생성자가 필수인 이유는? JPA 인터페이스의 구현체로 대부분 사용되는 Hibernate는 내부적으로 Class.newInstance()라는 Java..