SpringBoot RestTemplateリクエストツールクラス
22011 ワード
SpringBoot RestTemplateリクエストツールクラス
Naotu
要求ログブロック
@Slf4j
public class HttpLoggingInterceptor implements ClientHttpRequestInterceptor {
private static final String REQ_BEG = "=========================== req beg >>>>>>>>>>>>>>>>>>>>>>>>>>";
private static final String REQ_END = "=========================== req end <<<<<<<<<<<<<<<<<<<<<<<<<;
private static final String RES_BEG = "=========================== res beg >>>>>>>>>>>>>>>>>>>>>>>>>>";
private static final String RES_END = "=========================== res end <<<<<<<<<<<<<<<<<<<<<<<<<;
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
logRequest(request, body);
ClientHttpResponse response = null;
try {
response = execution.execute(request, body);
} catch (SocketTimeoutException e) {
// throw new BusinessException(EnumResult.CODE_800004, " ");
}
logResponse(response);
return response;
}
private void logRequest(HttpRequest request, byte[] body) throws IOException {
if (log.isInfoEnabled()) {
log.info(REQ_BEG);
log.info("URI : {}", request.getURI());
log.info("Method : {}", request.getMethod());
log.info("Headers : {}", request.getHeaders());
log.info("Request body: {}", new String(body, "UTF-8"));
log.info(REQ_END);
}
}
private void logResponse(ClientHttpResponse response) throws IOException {
if (log.isInfoEnabled()) {
log.info(RES_BEG);
log.info("Status code : {}", response.getStatusCode());
log.info("Status text : {}", response.getStatusText());
log.info("Headers : {}", response.getHeaders());
log.info("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
log.info(RES_END);
}
}
}
単一インスタンスRestTemplateの作成
@Getter
public enum RestEnum {
/**
* RestTemplate
*/
SINGLE_INSTANCE;
private RestTemplate restTemplate;
RestEnum() {
//
restTemplate = new RestTemplateBuilder()
.setConnectTimeout(Duration.ofMillis(5000))
.setReadTimeout(Duration.ofMillis(5000))
.build();
//
ClientHttpRequestInterceptor ri = new HttpLoggingInterceptor();
List<ClientHttpRequestInterceptor> ris = new ArrayList<>();
ris.add(ri);
restTemplate.setInterceptors(ris);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
}
}
ツールクラス
public class RestClient {
public static String postJson(String reqUrl, String reqJsonStrParam) {
// Header
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
//
HttpEntity<String> requestEntity = new HttpEntity<>(reqJsonStrParam, httpHeaders);
//
ResponseEntity<String> resp = RestEnum.SINGLE_INSTANCE.getRestTemplate()
.exchange(reqUrl, HttpMethod.POST, requestEntity, String.class);
//
return resp.getBody();
}
public static String postForm(String reqUrl, String reqFormPara) {
// Header
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//
HttpEntity<String> requestEntity = new HttpEntity<>(reqFormPara, httpHeaders);
//
ResponseEntity<String> resp = RestEnum.SINGLE_INSTANCE.getRestTemplate()
.exchange(reqUrl, HttpMethod.POST, requestEntity, String.class);
//
return resp.getBody();
}
}
テスト
String jsonStr = "{\"name\":\"BeJson\",\"url\"}";
RestClient.postJson("http://www.baidu.com", jsonStr);
テストログ
@Slf4j
public class HttpLoggingInterceptor implements ClientHttpRequestInterceptor {
private static final String REQ_BEG = "=========================== req beg >>>>>>>>>>>>>>>>>>>>>>>>>>";
private static final String REQ_END = "=========================== req end <<<<<<<<<<<<<<<<<<<<<<<<<;
private static final String RES_BEG = "=========================== res beg >>>>>>>>>>>>>>>>>>>>>>>>>>";
private static final String RES_END = "=========================== res end <<<<<<<<<<<<<<<<<<<<<<<<<;
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
logRequest(request, body);
ClientHttpResponse response = null;
try {
response = execution.execute(request, body);
} catch (SocketTimeoutException e) {
// throw new BusinessException(EnumResult.CODE_800004, " ");
}
logResponse(response);
return response;
}
private void logRequest(HttpRequest request, byte[] body) throws IOException {
if (log.isInfoEnabled()) {
log.info(REQ_BEG);
log.info("URI : {}", request.getURI());
log.info("Method : {}", request.getMethod());
log.info("Headers : {}", request.getHeaders());
log.info("Request body: {}", new String(body, "UTF-8"));
log.info(REQ_END);
}
}
private void logResponse(ClientHttpResponse response) throws IOException {
if (log.isInfoEnabled()) {
log.info(RES_BEG);
log.info("Status code : {}", response.getStatusCode());
log.info("Status text : {}", response.getStatusText());
log.info("Headers : {}", response.getHeaders());
log.info("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
log.info(RES_END);
}
}
}
@Getter
public enum RestEnum {
/**
* RestTemplate
*/
SINGLE_INSTANCE;
private RestTemplate restTemplate;
RestEnum() {
//
restTemplate = new RestTemplateBuilder()
.setConnectTimeout(Duration.ofMillis(5000))
.setReadTimeout(Duration.ofMillis(5000))
.build();
//
ClientHttpRequestInterceptor ri = new HttpLoggingInterceptor();
List<ClientHttpRequestInterceptor> ris = new ArrayList<>();
ris.add(ri);
restTemplate.setInterceptors(ris);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
}
}
public class RestClient {
public static String postJson(String reqUrl, String reqJsonStrParam) {
// Header
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
//
HttpEntity<String> requestEntity = new HttpEntity<>(reqJsonStrParam, httpHeaders);
//
ResponseEntity<String> resp = RestEnum.SINGLE_INSTANCE.getRestTemplate()
.exchange(reqUrl, HttpMethod.POST, requestEntity, String.class);
//
return resp.getBody();
}
public static String postForm(String reqUrl, String reqFormPara) {
// Header
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//
HttpEntity<String> requestEntity = new HttpEntity<>(reqFormPara, httpHeaders);
//
ResponseEntity<String> resp = RestEnum.SINGLE_INSTANCE.getRestTemplate()
.exchange(reqUrl, HttpMethod.POST, requestEntity, String.class);
//
return resp.getBody();
}
}
String jsonStr = "{\"name\":\"BeJson\",\"url\"}";
RestClient.postJson("http://www.baidu.com", jsonStr);