新增远程调用异常信息回显、处理ContentType问题
This commit is contained in:
parent
838bfe1ea2
commit
d28c1238c3
@ -3,6 +3,8 @@ package ink.wgink.common.advice;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import ink.wgink.exceptions.*;
|
||||
import ink.wgink.exceptions.base.SystemException;
|
||||
import ink.wgink.exceptions.rpc.RemoteRequestException;
|
||||
import ink.wgink.exceptions.rpc.RemoteResponseException;
|
||||
import ink.wgink.interfaces.consts.ISystemConstant;
|
||||
import ink.wgink.pojo.result.ErrorResult;
|
||||
import ink.wgink.util.AesUtil;
|
||||
@ -72,6 +74,10 @@ public class ResponseAdvice {
|
||||
result.setCode(ErrorResult.ErrorResultCodeEnum.PROPERTIES_ERROR.getValue());
|
||||
} else if (e instanceof AccessTokenException) {
|
||||
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
||||
} else if (e instanceof RemoteRequestException) {
|
||||
response.setStatus(ErrorResult.ErrorResultCodeEnum.REMOTE_REQUEST_EXCEPTION.getValue());
|
||||
} else if (e instanceof RemoteResponseException) {
|
||||
response.setStatus(ErrorResult.ErrorResultCodeEnum.REMOTE_RESPONSE_EXCEPTION.getValue());
|
||||
}
|
||||
// 自定义提示信息
|
||||
if (e instanceof SystemException) {
|
||||
|
@ -1,10 +1,13 @@
|
||||
package ink.wgink.common.rpc.rest.request;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import ink.wgink.exceptions.ParamsException;
|
||||
import ink.wgink.exceptions.rpc.RemoteRequestException;
|
||||
import ink.wgink.exceptions.rpc.RemoteResponseException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
@ -28,6 +31,7 @@ import java.util.Map;
|
||||
**/
|
||||
public class RestRemoteRequest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RestRemoteRequest.class);
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
public RestRemoteRequest() {
|
||||
@ -72,12 +76,7 @@ public class RestRemoteRequest {
|
||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.GET, httpEntity, String.class);
|
||||
return getStringResponse(responseEntity);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (e instanceof HttpClientErrorException) {
|
||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
||||
}
|
||||
throw new RemoteRequestException(e.getMessage(), e);
|
||||
return getErrorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,11 +93,7 @@ public class RestRemoteRequest {
|
||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.DELETE, httpEntity, String.class);
|
||||
return getStringResponse(responseEntity);
|
||||
} catch (Exception e) {
|
||||
if (e instanceof HttpClientErrorException) {
|
||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
||||
}
|
||||
throw new RemoteRequestException(e.getMessage(), e);
|
||||
return getErrorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -115,11 +110,7 @@ public class RestRemoteRequest {
|
||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.POST, httpEntity, String.class);
|
||||
return getStringResponse(responseEntity);
|
||||
} catch (Exception e) {
|
||||
if (e instanceof HttpClientErrorException) {
|
||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
||||
}
|
||||
throw new RemoteRequestException(e.getMessage(), e);
|
||||
return getErrorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -136,11 +127,7 @@ public class RestRemoteRequest {
|
||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.POST, httpEntity, String.class);
|
||||
return getStringResponse(responseEntity);
|
||||
} catch (Exception e) {
|
||||
if (e instanceof HttpClientErrorException) {
|
||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
||||
}
|
||||
throw new RemoteRequestException(e.getMessage(), e);
|
||||
return getErrorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -157,11 +144,7 @@ public class RestRemoteRequest {
|
||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.PUT, httpEntity, String.class);
|
||||
return getStringResponse(responseEntity);
|
||||
} catch (Exception e) {
|
||||
if (e instanceof HttpClientErrorException) {
|
||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
||||
}
|
||||
throw new RemoteRequestException(e.getMessage(), e);
|
||||
return getErrorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -176,11 +159,11 @@ public class RestRemoteRequest {
|
||||
HttpEntity<String> httpEntity;
|
||||
if (jsonBody != null) {
|
||||
HttpHeaders httpHeaders = getHttpHeaders(headerMap);
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||
if (jsonBody instanceof Collection) {
|
||||
httpEntity = new HttpEntity(JSONArray.toJSONString(jsonBody), getHttpHeaders(headerMap));
|
||||
httpEntity = new HttpEntity(JSONArray.toJSONString(jsonBody), httpHeaders);
|
||||
} else {
|
||||
httpEntity = new HttpEntity(JSONObject.toJSONString(jsonBody), getHttpHeaders(headerMap));
|
||||
httpEntity = new HttpEntity(JSONObject.toJSONString(jsonBody), httpHeaders);
|
||||
}
|
||||
} else if (formVariableParams != null) {
|
||||
HttpHeaders httpHeaders = getHttpHeaders(headerMap);
|
||||
@ -218,7 +201,30 @@ public class RestRemoteRequest {
|
||||
if (responseEntity.getStatusCode() != HttpStatus.OK) {
|
||||
throw new RemoteResponseException("远程调用响应状态码不支持: " + responseEntity.getStatusCode());
|
||||
}
|
||||
return responseEntity.getBody();
|
||||
String responseBody = responseEntity.getBody();
|
||||
LOG.debug("Response body: {}", responseBody);
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 失败响应
|
||||
*
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
public String getErrorResponse(Exception e) {
|
||||
if (e instanceof HttpClientErrorException) {
|
||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
||||
String errorBody = exception.getResponseBodyAsString();
|
||||
LOG.debug("Error body: {}", errorBody);
|
||||
JSONObject errorJSONBody = JSON.parseObject(errorBody);
|
||||
if (errorJSONBody.containsKey("code") && errorJSONBody.containsKey("detail") && errorJSONBody.containsKey("msg")) {
|
||||
throw new RemoteResponseException(errorJSONBody.getString("msg"));
|
||||
}
|
||||
throw new RemoteResponseException(errorBody);
|
||||
}
|
||||
throw new RemoteRequestException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user