본문 바로가기
프로그램/자바

자바 cors 우회하기 get,post방식

by cbwstar 2022. 7. 12.
728x90
반응형

get 방식 호출

java
닫기
​​자바 네이버 지도 get 방식 호출 처리 ​​ ​​@RequestMapping(value="/map-direction/v1/driving.do") ​​​​public ModelAndView driving(HttpServletRequest request) throws Exception{ ‌​​​​ ‌​​​​ModelAndView mav = new ModelAndView("jsonView"); ‌‌String start = EgovStringUtil.nvl(request.getParameter("start")); //시작좌표 ‌‌String goal = EgovStringUtil.nvl(request.getParameter("goal")); //목적지좌표 ‌‌String option = EgovStringUtil.nvl(request.getParameter("option")); ‌‌log.debug("start : " + start); ‌‌log.debug("goal : " + goal); ‌‌log.debug("option : " + option); ‌‌‌‌‌‌ ‌‌String apiUrl = "https://naveropenapi.apigw.ntruss.com/map-direction/v1/driving"; ‌‌ ‌‌// 네이버지도 OPEN API 호출 URL 정보 설정 ‌‌apiUrl += "?start="+start+"&goal="+goal+"&option="+option; ‌‌ ‌‌log.debug("네이버지도 apiUrl: " + apiUrl); ‌‌ ‌‌ ‌​​​​CloseableHttpClient httpClient = HttpClients.createDefault(); ‌​​​​ ‌​​​​HttpGet httpGet = new HttpGet(apiUrl); ‌​​​​ ​​​​​​​​/* 헤더값 설정 */ ‌​​​​httpGet.addHeader("Content-type","application/json"); ‌​​​​httpGet.addHeader("X-NCP-APIGW-API-KEY-ID","c"); ‌​​​​httpGet.addHeader("X-NCP-APIGW-API-KEY","HW"); ‌​​​​ ‌​​​​ ‌‌try { ‌‌‌ ‌‌‌//get 요청 ‌‌‌CloseableHttpResponse httpResponse = httpClient.execute(httpGet); ‌‌‌ ‌‌‌log.debug("Get Response status" + httpResponse.getStatusLine().getStatusCode()); ‌‌‌ ‌‌‌String json = EntityUtils.toString(httpResponse.getEntity(),"UTF-8"); ‌‌‌ ‌‌‌log.debug("응답결과:" + json); ‌‌‌ ‌‌‌mav.addObject("naverApi", json); //네이버지도 좌표 정보 ‌‌‌mav.addObject("ResultCode", "SUCCESS"); ‌‌‌mav.addObject("ErrorMsg", ""); ‌‌‌ ​​​​​​​​} catch (IOException e) { ‌​​​​​​​​mav.addObject("ResultCode", "ERROR"); ‌‌‌mav.addObject("ErrorMsg", ""); ​​​​​​​​​​ ​​​​​​​​} finally { ‌​​​​​​​​if (httpClient != null) { ‌‌​​​​​​​​httpClient.close(); ‌​​​​​​​​} ​​​​​​​​} ​​​​​​​​return mav; ‌‌‌‌ ​​​​}

post 방식 호출 처리

java
닫기
​​/* 사업자 진위확인조회 api */ ​​​​@RequestMapping(value = "/businessmanApiValidate.do") ​​​​public NexacroResult businessmanApiValidate( @ParamDataSet(name = "dsSearch", required = false) Map<String,String> searchInfo) { ​​​​​​​​NexacroResult result = new NexacroResult(); ​​​​​​​​String responseString=""; ​​​​​​​​List<Map<String,Object>> businessList = new ArrayList<Map<String,Object>>(); //사업자정보 ​​​​​​​​List<Map<String,Object>> requestParamList = new ArrayList<Map<String,Object>>(); //requestParamList ​​​​​​​​List<Map<String,Object>> statusList = new ArrayList<Map<String,Object>>(); //statusList ​​​​​​​​Map<String,Object> businessMap = new HashMap<String,Object>(); ​​​​​​​​Map<String,Object> requestParamMap = new HashMap<String,Object>(); ​​​​​​​​Map<String,Object> statusMap = new HashMap<String,Object>(); ​​​​​​​​try { ​​​​​​​​​​​​responseString = processHTTP2(searchInfo); ​​​​​​​​​​​​if(!responseString.equals("")) { ​​​​​​​​​​​​​​​​JSONObject jsonObject = JSONObject.fromObject(JSONSerializer.toJSON(responseString)); ​​​​​​​​​​​​​​​​JSONArray jsonArray = (JSONArray) jsonObject.get("data"); //사업자정보 ​​​​​​​​​​​​​​​​//사업자정보 map으로 변환 ​​​​​​​​​​​​​​​​if(jsonArray.size() > 0) { ​​​​​​​​​​​​​​​​​​​​JSONObject tempJson = jsonArray.getJSONObject(0); ​​​​​​​​​​​​​​​​​​​​//Map Parameter를 추가 ​​​​​​​​​​​​​​​​​​​​if(tempJson != null){ ​​​​​​​​​​​​​​​​​​​​​​​​@SuppressWarnings("unchecked") ​​​​​​​​​​​​​​​​​​​​​​​​Iterator<String> keys = tempJson.keySet().iterator(); //사업자정보 ​​​​​​​​​​​​​​​​​​​​​​​​while( keys.hasNext() ){ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​String key = keys.next(); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​if(!key.equals("request_param") && !key.equals("status") ) { ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​businessMap.put(key, tempJson.get(key)); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​} ​​​​​​​​​​​​​​​​​​​​​​​​} ​​​​​​​​​​​​​​​​​​​​​​​​//request_param ​​​​​​​​​​​​​​​​​​​​​​​​JSONObject requestParam = (JSONObject) tempJson.get("request_param"); ​​​​​​​​​​​​​​​​​​​​​​​​if(requestParam != null) { ​​​​​​​​​​​​​​​​​​​​​​​​​​​​@SuppressWarnings("unchecked") ​​​​​​​​​​​​​​​​​​​​​​​​​​​​Iterator<String> keys2 = requestParam.keySet().iterator(); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​while (keys2.hasNext()) { ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​String key = keys2.next(); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​requestParamMap.put(key, requestParam.get(key)); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​} ​​​​​​​​​​​​​​​​​​​​​​​​} ​​​​​​​​​​​​​​​​​​​​​​​​//request_param ​​​​​​​​​​​​​​​​​​​​​​​​JSONObject status = (JSONObject) tempJson.get("status"); ​​​​​​​​​​​​​​​​​​​​​​​​if(status != null) { ​​​​​​​​​​​​​​​​​​​​​​​​​​​​@SuppressWarnings("unchecked") ​​​​​​​​​​​​​​​​​​​​​​​​​​​​Iterator<String> keys3 = status.keySet().iterator(); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​while (keys3.hasNext()) { ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​String key = keys3.next(); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​statusMap.put(key, status.get(key)); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​} ​​​​​​​​​​​​​​​​​​​​​​​​} ​​​​​​​​​​​​​​​​​​​​} ​​​​​​​​​​​​​​​​} ​​​​​​​​​​​​} ​​​​​​​​​​​​businessList.add(businessMap); ​​​​​​​​​​​​requestParamList.add(requestParamMap); ​​​​​​​​​​​​statusList.add(statusMap); ​​​​​​​​​​​​log.debug("businessList:" + businessList); ​​​​​​​​​​​​log.debug("requestParamList:" + requestParamList); ​​​​​​​​​​​​log.debug("statusList:" + statusList); ​​​​​​​​​​​​result.addDataSet("output1", businessList); ​​​​​​​​​​​​result.addDataSet("output2", requestParamList); ​​​​​​​​​​​​result.addDataSet("output3", statusList); ​​​​​​​​} catch (HttpException e) { ​​​​​​​​​​​​log.debug("api 사업자 진위확인 Exception : " + e.getMessage()); ​​​​​​​​​​​​result.setErrorCode(-1); ​​​​​​​​​​​​result.setErrorMsg(" "); ​​​​​​​​} ​​​​​​​​return result; ​​​​} public String processHTTP2(Map<String,String> params ) throws HttpException ​​​​{ ​​​​​​​​int statusCode = 0; ​​​​​​​​String url = "http://api.odcloud.kr/api/nts-businessman/v1/validate?serviceKey=sic"; ​​​​​​​​CloseableHttpClient httpclient = HttpClientBuilder.create().build(); ​​​​​​​​HttpPost httpPost = new HttpPost(url); ​​​​​​​​String bNo = EgovStringUtil.nvl(params.get("b_no")); //사업자등록번호(필수) ​​​​​​​​String pNm = EgovStringUtil.nvl(params.get("p_nm")); //대표자성명(필수) ​​​​​​​​String startDt = EgovStringUtil.nvl(params.get("start_dt")); //개업일자(필수) ​​​​​​​​String pNm2 = EgovStringUtil.nvl(params.get("p_nm2")); //대표자성명2 ​​​​​​​​String bNm = EgovStringUtil.nvl(params.get("b_nm")); //상호 ​​​​​​​​String corpNo = EgovStringUtil.nvl(params.get("corp_no")); //법인등록번호 ​​​​​​​​String bSector = EgovStringUtil.nvl(params.get("b_sector")); //주업태명 ​​​​​​​​String bType = EgovStringUtil.nvl(params.get("b_type")); //주종목명 ​​​​​​​​String json = "{\"businesses\": [ { \"b_no\":\"" +bNo + "\","; //사업자등록번호(필수) ​​​​​​​​​​​​​​​json += "\"start_dt\":\"" +startDt + "\","; //개업일자(필수) ​​​​​​​​​​​​​​​json += "\"p_nm\":\"" +pNm + "\","; //대표자성명(필수) ​​​​​​​​​​​​​​​json += "\"p_nm2\":\"" + pNm2 + "\","; //대표자성명2 ​​​​​​​​​​​​​​​json += "\"b_nm\":\"" + bNm + "\","; //상호 ​​​​​​​​​​​​​​​json += "\"corp_no\":\"" + corpNo + "\","; //법인등록번호 ​​​​​​​​​​​​​​​json += "\"b_sector\":\"" + bSector + "\","; //주업태명 ​​​​​​​​​​​​​​​json += "\"b_type\":\"" + bType + "\""; //주종목명 ​​​​​​​​​​​​​​​json += "}]}"; ​​​​​​​​​​​​// log.debug("json" + json); ​​​​​​​​StringEntity requestEntity = new StringEntity(json , "utf-8"); ​​​​​​​ ​​​​​​​​/* 헤더값 설정 */ ​​​​​​​​httpPost.addHeader("content-type","application/json"); ​​​​​​​​httpPost.addHeader("Accept","application/json"); ​​​​​​​​httpPost.setEntity(requestEntity); ​​​​​​​​BufferedReader rd= new BufferedReader(new InputStreamReader(System.in)); //초기화 ​​​​​​​​try { ​​​​​​​​​​​​HttpResponse response = httpclient.execute(httpPost); ​​​​​​​​​​​​statusCode = response.getStatusLine().getStatusCode(); ​​​​​​​​​​​​if( statusCode == 200){ ​​​​​​​​​​​​​​​​rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); ​​​​​​​​​​​​​​​​// 정상인 경우 메시지를 읽어 들여 그 결과를 리턴한다. ​​​​​​​​​​​​​​​​StringBuffer result = new StringBuffer(); ​​​​​​​​​​​​​​​​String line = ""; ​​​​​​​​​​​​​​​​while ((line = rd.readLine()) != null) { ​​​​​​​​​​​​​​​​​​​​result.append(line); ​​​​​​​​​​​​​​​​} ​​​​​​​​​​​​​​​​​log.debug("result.toString(): " + result.toString() ); ​​​​​​​​​​​​​​​​return result.toString(); ​​​​​​​​​​​​} ​​​​​​​​} catch (Throwable e) { ​​​​​​​​​​​​throw new HttpException((new StringBuilder("오류가 발생하였습니다.")).append(statusCode).toString()); ​​​​​​​​} finally { ​​​​​​​​​​​​try { ​​​​​​​​​​​​​​​​if (rd != null) ​​​​​​​​​​​​​​​​​​​​rd.close(); ​​​​​​​​​​​​} catch (IOException e) { ​​​​​​​​​​​​​​​​throw new HttpException((new StringBuilder("오류가 발생하였습니다.")).append(statusCode).toString()); ​​​​​​​​​​​​} ​​​​​​​​} ​​​​​​​​if( statusCode != 200) ​​​​​​​​{ ​​​​​​​​​​​​throw new HttpException((new StringBuilder("오류가 발생하였습니다.")).append(statusCode).toString()); ​​​​​​​​} ​​​​​​​​return ""; ​​​​}
728x90
반응형


"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."