시스템 분산 목적으로 중계 서버를 구현하기로 했음.
@RequestMapping(value = "/bank/info", method = RequestMethod.POST)
public CrsResult bankInfo(HttpServletRequest request,
@RequestHeader Map<String, String> headers,
@RequestBody(required = false) Map<String, Object> params,
HttpServletResponse response) throws Exception {
return httpRequest.request(request, response, headers, params);
}
IHttpRequest
public interface IHttpRequest {
CrsResult request(HttpServletRequest request, HttpServletResponse response, Map<String, String> headers, Map<String, ?> params);
UploadResult requestUpload(HttpServletRequest request, HttpServletResponse response, Map<String, String> headers, Map<String, ?> params);
}
IHttpRequestImpl
@Service
public class IHttpRequestImpl implements IHttpRequest {
@Autowired RestApiClient restApiClient;
@Override
public CrsResult request(HttpServletRequest request, HttpServletResponse response, Map<String, String> headers, Map<String, ?> params) {
try {
return (CrsResult) request(request, response, headers, params, CrsResult.class).getBody();
} catch (Exception ex) {
Log.error("request Error : {}", ex.getMessage());
}
return new CrsResult("-1", "Connection Failed..");
}
@Override
public UploadResult requestUpload(HttpServletRequest request, HttpServletResponse response, Map<String, String> headers, Map<String, ?> params) {
try {
return (UploadResult) request(request, response, headers, params, UploadResult.class).getBody();
} catch (Exception ex) {
Log.error("requestUpload Error : {}", ex.getMessage());
}
return new UploadResult("", "", "");
}
private void addHeader(HttpServletResponse response, HttpHeaders httpHeaders) {
for(String key : httpHeaders.keySet()) {
response.setHeader(key, String.valueOf(httpHeaders.get(key).get(0)));
}
}
private ResponseEntity request(HttpServletRequest request, HttpServletResponse response, Map<String, String> headers, Map<String, ?> params, Class<?> responseType) throws IOException {
ResponseEntity result = restApiClient.request(request, headers, params, responseType);
addHeader(response, result.getHeaders());
return result;
}
}
RestApiClient
public ResponseEntity request(HttpServletRequest request, Map<String, String> headers, Map<String, ?> params, Class<?> responseType) throws IOException {
Log.info("request url : {}", request.getRequestURI());
Log.info("header : {}", headers);
URI uri = getUri(request.getRequestURI());
RequestEntity requestEntity;
ResponseEntity<?> responseEntity;
if(request.getMethod().equalsIgnoreCase("POST")) {
requestEntity = getRequestEntity(uri, HttpMethod.POST, headers, params);
responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, responseType);
} else {
requestEntity = getRequestEntity(uri, HttpMethod.GET, headers, params);
responseEntity = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, responseType);
}
return responseEntity;
}
private URI getUri(String url) {
URI uri = null;
try {
uri = new URI(baseUrl + url);
} catch (URISyntaxException e) {
e.printStackTrace();
}
return uri;
}
private RequestEntity getRequestEntity(URI uri, HttpMethod method, Map<String, String> headers, Object body) {
MultiValueMap<String, String> headerParams = new LinkedMultiValueMap<>();
if (headers != null) headerParams.setAll(headers);
return new RequestEntity(body, headerParams, method, uri);
}
HttpClientInterceptor
public class HttpClientInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
Log.info("request : {}", request.getURI());
ClientHttpResponse response = execution.execute(request, body);
return response;
}
}
보안 부분과 인증키 조합, 오류 부분은 공개하지 않음.
지금 try 문도 로그를 처리하고 상위 핸들러로 정보를 보내야 한다.
'포트폴리오 > Java rest api' 카테고리의 다른 글
Reuters 환율 처리 (1) | 2022.11.18 |
---|---|
rest api filter에서 header, body 변경 (0) | 2022.09.07 |
rest api + jpa(jpql) + replica set (0) | 2022.08.02 |
자바 날씨 API (0) | 2021.06.09 |
Java 기본 Rest api (0) | 2020.08.11 |