Quellcode durchsuchen

KC인증번호 조회 추가

gagamel vor 5 Jahren
Ursprung
Commit
6e5fd4e0e4

+ 85 - 0
style24.core/src/main/java/com/style24/core/biz/thirdparty/SafetyKoreaApi.java

@@ -0,0 +1,85 @@
+package com.style24.core.biz.thirdparty;
+
+import java.net.URI;
+
+import javax.annotation.PostConstruct;
+
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * KC인증번호 조회 (www.safetykorea.kr open API)
+ * 
+ * @author gagamel
+ * @since 2020. 12. 1
+ */
+@Component
+@Slf4j
+public class SafetyKoreaApi {
+
+	// URL
+	private String apiUrl = "http://www.safetykorea.kr/openapi/api/cert/certificationList.json";
+
+	// 인증키
+	private String authKey = "34fa9888-a2fb-4c02-adf4-18f018bb936d";
+
+	@Autowired
+	private RestTemplate restTemplate;
+
+	@PostConstruct
+	public void init() {
+		log.debug("\n\n---- SafetyKoreaApi initialization started ----");
+		log.debug("apiUrl: [{}]", apiUrl);
+		log.debug("authKey: [{}]", authKey);
+		log.debug("\n--- SafetyKoreaApi initialization completed ----\n");
+	}
+
+	/**
+	 * KC인증번호 조회
+	 * @param goodsCd - 상품코드
+	 * @return 인증번호
+	 * @throws Exception
+	 * @author gagamel
+	 * @since 2020. 12. 1
+	 */
+	public String getKoreaCertifyNo(String goodsCd) throws Exception {
+		// Parameter
+		MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
+		params.add("conditionKey", "all");
+		params.add("conditionValue", goodsCd);
+		log.info("params: {}", params);
+
+		// Header
+		HttpHeaders headers = new HttpHeaders();
+		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+		headers.add("AuthKey", authKey);
+
+		URI url = URI.create(apiUrl);
+
+		// GET방식으로 호출
+		HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params, headers);
+		ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
+		log.info("responseEntity.getStatusCode(): {} ", responseEntity.getStatusCode());
+
+		String responseJson = responseEntity.getBody();
+		log.info("responseEntity.getBody(): {}", responseJson);
+
+		JSONParser jsonParser = new JSONParser();
+		JSONObject jObj = (JSONObject)jsonParser.parse(responseJson);
+
+		return jObj.get("certNum").toString();
+	}
+
+}