이호영 이호영 2025-04-01
카카오톡아이디 확인 null Exception 수정
@d0decaee4324b248af939e01e1d24e1259592da4
src/main/java/itn/let/kakao/kakaoComm/kakaoApi/KakaoApiProfile.java
--- src/main/java/itn/let/kakao/kakaoComm/kakaoApi/KakaoApiProfile.java
+++ src/main/java/itn/let/kakao/kakaoComm/kakaoApi/KakaoApiProfile.java
@@ -1,5 +1,7 @@
 package itn.let.kakao.kakaoComm.kakaoApi;
 
+import java.util.Optional;
+
 import javax.annotation.Resource;
 
 import org.apache.http.HttpResponse;
@@ -229,23 +231,23 @@
 					
 					JSONObject templateProfile = (JSONObject) object.get("data");
 					
-					String senderKey = (templateProfile.get("senderKey") == null) ? null : templateProfile.get("senderKey").toString();						//발신프로필키
-					String uuid = (templateProfile.get("uuid") == null) ? null : templateProfile.get("uuid").toString();									//카카오톡 채널
-					String name = (templateProfile.get("name") == null) ? null : templateProfile.get("name").toString();									//카카오톡 채널 발신프로필 명
-					String status = (templateProfile.get("status") == null) ? null : templateProfile.get("status").toString();								//발신프로필 상태
-					boolean block = (boolean) templateProfile.get("block");																					//발신프로필 차단 여부
-					boolean dormant = (boolean) templateProfile.get("dormant");																				//발신프로필 휴면 여부
-					String profileStatus = (templateProfile.get("profileStatus") == null) ? null : templateProfile.get("profileStatus").toString();			//카카오톡 채널 상태((A: activated, C: deactivated, B: block, E: deleting, D: deleted)
-					String createdAt = (templateProfile.get("createdAt") == null) ? null : templateProfile.get("createdAt").toString();						//발신프로필 등록일
-					String modifiedAt = (templateProfile.get("modifiedAt") == null) ? null : templateProfile.get("modifiedAt").toString();					//최종수정일
-					String categoryCode = (templateProfile.get("categoryCode") == null) ? null : templateProfile.get("categoryCode").toString();			//발신프로필 카테고리코드
-					boolean alimtalk = (boolean) templateProfile.get("alimtalk");																			//알림톡 사용 여부
-					boolean bizchat = (boolean) templateProfile.get("bizchat");																				//상담톡 사용 여부
-					boolean brandtalk = (boolean) templateProfile.get("brandtalk");																			//브랜드톡 사용 여부
-					String committalCompanyName = (templateProfile.get("committalCompanyName") == null) ? null : templateProfile.get("committalCompanyName").toString(); 														//위탁사 이름(상담톡 관련)
-					String channelKey = (templateProfile.get("channelKey") == null) ? null : templateProfile.get("channelKey").toString();					//메시지 전송 결과 수신 채널키
-					boolean businessProfile = (boolean) templateProfile.get("businessProfile");																//카카오톡 채널 비즈니스 인증 여부
-					String businessType = (templateProfile.get("businessType") == null) ? null : templateProfile.get("businessType").toString();			//카카오톡 채널 비즈니스 인증 타입
+					String senderKey = getStringValue(templateProfile, "senderKey");						//발신프로필키
+					String uuid = getStringValue(templateProfile, "uuid");									//카카오톡 채널
+					String name = getStringValue(templateProfile, "name");									//카카오톡 채널 발신프로필 명
+					String status = getStringValue(templateProfile, "status");								//발신프로필 상태
+					boolean block = extractBoolean(templateProfile,"block", false);							//발신프로필 차단 여부
+					boolean dormant = extractBoolean(templateProfile, "dormant", false);					//발신프로필 휴면 여부
+					String profileStatus = getStringValue(templateProfile, "profileStatus");				//카카오톡 채널 상태((A: activated, C: deactivated, B: block, E: deleting, D: deleted)
+					String createdAt = getStringValue(templateProfile, "createdAt");						//발신프로필 등록일
+					String modifiedAt = getStringValue(templateProfile, "modifiedAt");						//최종수정일
+					String categoryCode = getStringValue(templateProfile, "categoryCode");					//발신프로필 카테고리코드
+					boolean alimtalk = extractBoolean(templateProfile, "alimtalk", false);					//알림톡 사용 여부
+					boolean bizchat =extractBoolean(templateProfile, "bizchat", false);						//상담톡 사용 여부
+					boolean brandtalk = extractBoolean(templateProfile, "brandtalk", false);				//브랜드톡 사용 여부
+					String committalCompanyName = getStringValue(templateProfile, "committalCompanyName");	//위탁사 이름(상담톡 관련)
+					String channelKey = getStringValue(templateProfile, "channelKey");						//메시지 전송 결과 수신 채널키
+					boolean businessProfile = extractBoolean(templateProfile, "businessProfile", false);	//카카오톡 채널 비즈니스 인증 여부
+					String businessType = getStringValue(templateProfile, "businessType");					//카카오톡 채널 비즈니스 인증 타입
 					
 
 					/*if(templateProfile.get("committalCompanyName") != null) {//위탁사 이름(상담톡 관련) 데이터가 NULL 로 넘어오는 경우가 있어서 예외처리해줌
@@ -290,4 +292,16 @@
 		return kakaoReturnVO;
 	}
 	
+	// 헬퍼 메서드
+	private String getStringValue(JSONObject json, String key) {
+		return Optional.ofNullable(json.get(key))
+				.map(Object::toString)
+				.orElse(null);
+	}
+	// Boolean 값 추출 헬퍼 메서드 (기본값 포함)
+	private boolean extractBoolean(JSONObject json, String key, boolean defaultValue) {
+		return Optional.ofNullable(json.get(key))
+			.map(value -> (boolean) value)  // boolean으로 캐스팅
+			.orElse(defaultValue);          // 키가 없으면 기본값 반환
+	}
 }
Add a comment
List