티스토리 뷰

 

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import kr.co.juvis.counsel.common.exception.InternalServerErrorException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author reference-m1
 * @desc RESTful API 공통
 */
@Slf4j
public class RESTfulAPI {
    
    /**
     * RESTful API GET
     * @param url
     * @param reqMap
     * @return
     * @throws JsonProcessingException
     * @throws RuntimeException
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> get(String url, Map<String, Object> reqMap) throws JsonProcessingException, RuntimeException {

        HttpMethod httpMethod = HttpMethod.GET;

        log.info("/******** RESTful API Call ********/");
        log.info("/** RequestMethod : {}", httpMethod);
        log.info("/** URL           : {}", url);
        log.info("/** Params        : {}", reqMap);
        log.info("/**********************************/");

        ObjectMapper mapper = new ObjectMapper();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);

        if (null != reqMap) {
            for (Map.Entry<String, Object> entry : reqMap.entrySet()) {
                builder = builder.queryParam(entry.getKey(), entry.getValue());
            }
        }

        ResponseEntity<String> response = new RestTemplate().exchange(builder.toUriString(), httpMethod, new HttpEntity<String>(headers), String.class);

        if (HttpStatus.OK != response.getStatusCode()) {

            String errMsg = String.format("RESTful API(%s) 연동시 문제가 발생했습니다. \nStatus code : %s \nURL : %s", httpMethod, response.getStatusCode(), url);
            log.error(errMsg);

            throw new InternalServerErrorException(errMsg);
        }

        return mapper.readValue(response.getBody(), Map.class);
    }

    /**
     * RESTful API POST
     * @param url
     * @param obj List<Map<String, Object>>, List<String>, Map<String, Object> 객체만 사용 가능
     * @return
     * @throws JsonProcessingException
     * @throws RuntimeException
     */
    public static Map<String, Object> post(String url, Object obj) throws JsonProcessingException, RuntimeException {
        return send(url, HttpMethod.POST, obj);
    }

    /**
     * RESTful API PUT
     * @param url
     * @param obj List<Map<String, Object>>, List<String>, Map<String, Object> 객체만 사용 가능
     * @return
     * @throws JsonProcessingException
     * @throws RuntimeException
     */
    public static Map<String, Object> put(String url, Object obj) throws JsonProcessingException, RuntimeException {
        return send(url, HttpMethod.PUT, obj);
    }

    /**
     * RESTful API DELETE
     * @param url
     * @param obj - List<Map<String, Object>>, List<String>, Map<String, Object> 객체만 사용 가능
     * @return
     * @throws JsonProcessingException
     * @throws RuntimeException
     */
    public static Map<String, Object> delete(String url, Object obj) throws JsonProcessingException, RuntimeException {
        return send(url, HttpMethod.DELETE, obj);
    }

    /**
     * RESTful API POST/PUT/DELETE
     * @param url
     * @param httpMethod
     * @param obj
     * @return
     * @throws JsonProcessingException
     * @throws RuntimeException
     */
    @SuppressWarnings("unchecked")
    private static Map<String, Object> send(String url, HttpMethod httpMethod, Object obj) throws JsonProcessingException, RuntimeException {

        log.info("/******** RESTful API Call ********/");
        log.info("/** RequestMethod : {}", httpMethod);
        log.info("/** URL           : {}", url);
        log.info("/**********************************/");
        
        ObjectMapper mapper = new ObjectMapper();
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<?> entity = null;
        
        if (obj instanceof Map) {
            entity = new HttpEntity<Map<String, Object>>((Map<String, Object>)obj, headers);
        } else if (obj instanceof List) {
            entity = new HttpEntity<List<Map<String, Object>>>((List<Map<String, Object>>)obj, headers);
        } else {
            entity = new HttpEntity<List<String>>((List<String>)obj, headers);
        }

        ResponseEntity<String> response = new RestTemplate().exchange(url, httpMethod, entity, String.class);

        if (HttpStatus.OK != response.getStatusCode()) {
            
            String errMsg = String.format("RESTful API(%s) 연동시 문제가 발생했습니다. \nStatus code : %s \nURL : %s", httpMethod, response.getStatusCode(), url);
            log.error(errMsg);

            throw new InternalServerErrorException(errMsg);
        }

        return mapper.readValue(response.getBody(), Map.class);
    }
}

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함