net.ellord.restAPI.service
> RestAPIService.java
net.ellord.restAPI.service.impl
> RestAPIDAO.java
> RestAPIServiceImpl.java
net.ellord.restAPI.vo
> ResponseVO.java
net.ellord.restAPI.web
> RestAPIController.java
전자정부 : ServiceImpl
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpUrlConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendear;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
@Service("restAPIService")
public class RestAPIServiceImpl extends EgovAbstractServiceImpl implemetns RestAPIService {
@Resource(name="restAPIDAO");
RestAPIDAO restAPIDAO;
private ObjectManager objectManager = new ObjectManager();
private final String targetURL = "https://목적지";
@Override
public void sendToMethod() throws Exception{
List<Map<String, Object>> resultList = restAPIDAO.getList();
JSONArray json_array = new JSONArray();
for(Map<String, Object> map : resultList){
json_array.add(map);
}//for
JSONObject jo = new JSONObject();
jo.put("sendList", json_array)
try{
String apiRes = "";
apiRes = callSendApi(jo, targetURL);
//리턴값을 vo에 매핑
ObjectMapper mp = new ObjectMapper();
ResponseVO vo = mp.readValue(apiRes, ResponseVO.class);
}catch(Exception e){
e.printStackTrace();
}
}
private String callSendApi(JSONObject jsonBody, String targetURL){
String rtn = null;
String line = null;
HttpsURLConnection httpsConn = null;
try {
URL url = new URL(targetURL);
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
httpsConn = (HttpsURLConnection) url.openConnection();
httpsConn.setRequestMethod("POST");
httpsConn.setRequestProperty("Content-Type", "application/json; utf-8");
httpsConn.setRequestProperty("Accept", "application/json");
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs,
String authType){
}
public void checkServerrTrusted(X509Certificate[] certs,
String authType){
}
}
};
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);
httpsConn.setUseCaches(false);
httpsConn.setReadTimeout(30000);
httpsConn.setConnectTimeout(30000);
SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(null, null, null);
httpsConn.setSSLSocketFactory(context.getSocketFactory());
String jsonData = objectManager.writeValueAsString(jsonBody);
OutputStream os = HttpsConn.getOutputStream();
os.write(jsonData.getBytes("UTF-8"));
os.flush();
os.close();
httpsConn.connect();
httpsConn.setInstanceFollowRedirects(true);
int responseCode = httpsConn.getResponseCode();
System.out("응답코드: " + responseCode);
System.out("응답메시지: " + httpsConn.getResponseMessage());
System.out("Http Msg: " + HttpsURLConnection.HTTP_OK);
if(responseCode == HttpsURLConnection.HTTP_OK) {
StringBuffer response = new StringBuffer();
BufferedReader res = new BufferedReader(new InputStreamReader(httpsConn.getInputStream(), "UTF-8"));
while( (line = res.readLine()) != null) {
response.append(line);
}
res.close();
rtn = response.toString();
}else{
StringBuffer response = new StringBuffer();
BufferedReader res = new BufferedReader(new InputStreamReader(httpsConn.getInputStream(), "UTF-8"));
while( (line = res.readLine()) != null) {
response.append(line);
}
res.close();
rtn = response.toString();
}
} catch (MalformedURLException e){
e.printStackTrace();
} catch (SocketTimeoutException e ){
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
} finally {
if(httpsConn != null){
httpsConn.disconnect();
}
}
return rtn;
}
}
'REST API' 카테고리의 다른 글
| JWT Create Token (0) | 2026.05.22 |
|---|---|
| REST API Controller : 전자정부 Controller (0) | 2026.05.22 |
| HTTPS REST API POST : 전자정부 DAO (0) | 2026.05.22 |
| HTTPS REST API POST : 전자정부 Service (0) | 2026.05.22 |