完善rest请求方法
This commit is contained in:
parent
ab58ad626e
commit
e60b80b664
@ -17,7 +17,7 @@ import org.springframework.web.client.RestTemplate;
|
||||
* @Date: 2021/3/31 12:18 下午
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public abstract class AbstractRestTemplate<SuccessResultType, PostBodyType> {
|
||||
public abstract class AbstractRestTemplate<SuccessResultType, BodyType> {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AbstractRestTemplate.class);
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@ -55,11 +55,11 @@ public abstract class AbstractRestTemplate<SuccessResultType, PostBodyType> {
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求主体
|
||||
* 请求主体
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract PostBodyType postBody();
|
||||
public abstract BodyType body();
|
||||
|
||||
/**
|
||||
* 成功返回类型
|
||||
@ -97,6 +97,35 @@ public abstract class AbstractRestTemplate<SuccessResultType, PostBodyType> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE请求
|
||||
*
|
||||
* @param url
|
||||
*/
|
||||
public void delete(String url) {
|
||||
Class<SuccessResultType> successResultTypeClass = successResultType();
|
||||
if (successResultTypeClass == null) {
|
||||
LOG.error("method successResultType can not return null");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
ResponseEntity<SuccessResultType> 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<SuccessResultType, PostBodyType> {
|
||||
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<SuccessResultType> 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<SuccessResultType, PostBodyType> {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
HttpEntity<PostBodyType> httpEntity = new HttpEntity<>(postBodyType, isJson ? jsonHeaders() : formHeaders());
|
||||
HttpEntity<BodyType> httpEntity = new HttpEntity<>(bodyType, isJson ? jsonHeaders() : formHeaders());
|
||||
ResponseEntity<SuccessResultType> responseEntity = restTemplate.postForEntity(url, httpEntity, successResultTypeClass);
|
||||
SuccessResultType result = responseEntity.getBody();
|
||||
if (HttpStatus.OK.value() == responseEntity.getStatusCodeValue()) {
|
||||
@ -153,6 +191,8 @@ public abstract class AbstractRestTemplate<SuccessResultType, PostBodyType> {
|
||||
public abstract void success200(SuccessResultType result);
|
||||
|
||||
/**
|
||||
* 错误
|
||||
*
|
||||
* @param error
|
||||
*/
|
||||
public abstract void error400(HttpClientErrorException error);
|
||||
|
Loading…
Reference in New Issue
Block a user