티스토리 뷰
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);
}
}
'프로그래밍 > Back end' 카테고리의 다른 글
[Back end] JDK, JRE 무엇이 다른가? 그리고 JVM (0) | 2019.07.08 |
---|---|
[Back end] Spring log4j, logback 중복 로그 (0) | 2019.07.08 |
[Back end] Java 부동소수점 산술 연산 주의사항 (0) | 2019.05.26 |
[Back end] Java의 특징 (0) | 2019.05.19 |
[Back end] Java 접근 제어자 및 각종 제어자 (0) | 2019.05.10 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
링크
TAG
- 오라클 내장 함수
- 소프트웨어공학
- Maven
- Collection
- React
- 정렬 알고리즘
- 리눅스 명령어
- 자바스크립트
- 경력관리
- SQL
- 제주도 여행
- 리액트 16
- 오라클
- sort algorithm
- 리액트
- 자바
- effective java
- 이직
- 성능분석
- 회고
- 제주도 3박4일 일정
- Linux 명령어
- Tomcat
- spring
- javascript
- 개발환경
- Java
- 프로그래머스
- Eclipse
- 프로그래머
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함