From 21d1f1647f0fc5ce72c90dff0c12c55da510d072 Mon Sep 17 00:00:00 2001 From: wanggeng888 <450292408@qq.com> Date: Wed, 31 Mar 2021 21:39:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Erest=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/http/AbstractRestTemplate.java | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 cloud-common/src/main/java/com/cm/common/utils/http/AbstractRestTemplate.java diff --git a/cloud-common/src/main/java/com/cm/common/utils/http/AbstractRestTemplate.java b/cloud-common/src/main/java/com/cm/common/utils/http/AbstractRestTemplate.java new file mode 100644 index 0000000..a7da712 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/utils/http/AbstractRestTemplate.java @@ -0,0 +1,160 @@ +package com.cm.common.utils.http; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.*; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: AbstractRestTemplateUtil + * @Description: + * @Author: wanggeng + * @Date: 2021/3/31 12:18 下午 + * @Version: 1.0 + */ +public abstract class AbstractRestTemplate { + private static final Logger LOG = LoggerFactory.getLogger(AbstractRestTemplate.class); + private RestTemplate restTemplate; + + public AbstractRestTemplate() { + SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(); + simpleClientHttpRequestFactory.setConnectTimeout(20 * 1000); + simpleClientHttpRequestFactory.setReadTimeout(60 * 1000); + this.restTemplate = new RestTemplate(simpleClientHttpRequestFactory); + } + + /** + * 请求头 + * + * @return + */ + public HttpHeaders headers() { + return new HttpHeaders(); + } + + /** + * json请求头 + * + * @return + */ + public HttpHeaders jsonHeaders() { + HttpHeaders httpHeaders = headers(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + return httpHeaders; + } + + public HttpHeaders formHeaders() { + HttpHeaders httpHeaders = headers(); + httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + return httpHeaders; + } + + /** + * POST请求主体 + * + * @return + */ + public abstract PostBodyType postBody(); + + /** + * 成功返回类型 + * + * @return + */ + public abstract Class successResultType(); + + /** + * GET请求 + * + * @param url + */ + public void get(String url) { + Class successResultTypeClass = successResultType(); + if (successResultTypeClass == null) { + LOG.error("method successResultType can not return null"); + return; + } + try { + ResponseEntity responseEntity = restTemplate.getForEntity(url, successResultTypeClass); + SuccessResultType result = responseEntity.getBody(); + if (HttpStatus.OK.value() == responseEntity.getStatusCodeValue()) { + success200(result); + } + } catch (Exception e) { + if (e instanceof HttpClientErrorException) { + HttpClientErrorException exception = (HttpClientErrorException) e; + if (exception.getRawStatusCode() == HttpStatus.BAD_REQUEST.value()) { + error400(exception); + return; + } + } + e.printStackTrace(); + } + } + + /** + * POST提交FORM + * + * @param url + */ + public void post(String url) { + post(url, false); + } + + /** + * POST提交JSON + * + * @param url + */ + public void postJson(String url) { + post(url, true); + } + + private void post(String url, boolean isJson) { + PostBodyType postBodyType = postBody(); + Class successResultTypeClass = successResultType(); + if (postBodyType == null) { + LOG.error("method postBody can not return null"); + return; + } + if (successResultTypeClass == null) { + LOG.error("method successResultType can not return null"); + return; + } + try { + HttpEntity httpEntity = new HttpEntity<>(postBodyType, isJson ? jsonHeaders() : formHeaders()); + ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, successResultTypeClass); + SuccessResultType result = responseEntity.getBody(); + if (HttpStatus.OK.value() == responseEntity.getStatusCodeValue()) { + success200(result); + } + } catch (Exception e) { + if (e instanceof HttpClientErrorException) { + HttpClientErrorException exception = (HttpClientErrorException) e; + if (exception.getRawStatusCode() == HttpStatus.BAD_REQUEST.value()) { + error400(exception); + return; + } + } + e.printStackTrace(); + } + } + + /** + * 成功结果 + * + * @param result + */ + public abstract void success200(SuccessResultType result); + + /** + * @param error + */ + public abstract void error400(HttpClientErrorException error); + +}