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 index a7da712..a12c276 100644 --- 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 @@ -17,7 +17,7 @@ import org.springframework.web.client.RestTemplate; * @Date: 2021/3/31 12:18 下午 * @Version: 1.0 */ -public abstract class AbstractRestTemplate { +public abstract class AbstractRestTemplate { private static final Logger LOG = LoggerFactory.getLogger(AbstractRestTemplate.class); private RestTemplate restTemplate; @@ -55,11 +55,11 @@ public abstract class AbstractRestTemplate { } /** - * POST请求主体 + * 请求主体 * * @return */ - public abstract PostBodyType postBody(); + public abstract BodyType body(); /** * 成功返回类型 @@ -97,6 +97,35 @@ public abstract class AbstractRestTemplate { } } + /** + * DELETE请求 + * + * @param url + */ + public void delete(String url) { + Class successResultTypeClass = successResultType(); + if (successResultTypeClass == null) { + LOG.error("method successResultType can not return null"); + return; + } + try { + ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, null, 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 * @@ -115,10 +144,19 @@ public abstract class AbstractRestTemplate { post(url, true); } + /** + * put + * + * @param url + */ + public void put(String url) { + post(url, true); + } + private void post(String url, boolean isJson) { - PostBodyType postBodyType = postBody(); + BodyType bodyType = body(); Class successResultTypeClass = successResultType(); - if (postBodyType == null) { + if (bodyType == null) { LOG.error("method postBody can not return null"); return; } @@ -127,7 +165,7 @@ public abstract class AbstractRestTemplate { return; } try { - HttpEntity httpEntity = new HttpEntity<>(postBodyType, isJson ? jsonHeaders() : formHeaders()); + HttpEntity httpEntity = new HttpEntity<>(bodyType, isJson ? jsonHeaders() : formHeaders()); ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, successResultTypeClass); SuccessResultType result = responseEntity.getBody(); if (HttpStatus.OK.value() == responseEntity.getStatusCodeValue()) { @@ -153,6 +191,8 @@ public abstract class AbstractRestTemplate { public abstract void success200(SuccessResultType result); /** + * 错误 + * * @param error */ public abstract void error400(HttpClientErrorException error);