新增远程调用异常信息回显、处理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 com.alibaba.fastjson.JSON;
|
||||||
import ink.wgink.exceptions.*;
|
import ink.wgink.exceptions.*;
|
||||||
import ink.wgink.exceptions.base.SystemException;
|
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.interfaces.consts.ISystemConstant;
|
||||||
import ink.wgink.pojo.result.ErrorResult;
|
import ink.wgink.pojo.result.ErrorResult;
|
||||||
import ink.wgink.util.AesUtil;
|
import ink.wgink.util.AesUtil;
|
||||||
@ -72,6 +74,10 @@ public class ResponseAdvice {
|
|||||||
result.setCode(ErrorResult.ErrorResultCodeEnum.PROPERTIES_ERROR.getValue());
|
result.setCode(ErrorResult.ErrorResultCodeEnum.PROPERTIES_ERROR.getValue());
|
||||||
} else if (e instanceof AccessTokenException) {
|
} else if (e instanceof AccessTokenException) {
|
||||||
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
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) {
|
if (e instanceof SystemException) {
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package ink.wgink.common.rpc.rest.request;
|
package ink.wgink.common.rpc.rest.request;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import ink.wgink.exceptions.ParamsException;
|
import ink.wgink.exceptions.ParamsException;
|
||||||
import ink.wgink.exceptions.rpc.RemoteRequestException;
|
import ink.wgink.exceptions.rpc.RemoteRequestException;
|
||||||
import ink.wgink.exceptions.rpc.RemoteResponseException;
|
import ink.wgink.exceptions.rpc.RemoteResponseException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
@ -28,6 +31,7 @@ import java.util.Map;
|
|||||||
**/
|
**/
|
||||||
public class RestRemoteRequest {
|
public class RestRemoteRequest {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(RestRemoteRequest.class);
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
public RestRemoteRequest() {
|
public RestRemoteRequest() {
|
||||||
@ -72,12 +76,7 @@ public class RestRemoteRequest {
|
|||||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.GET, httpEntity, String.class);
|
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.GET, httpEntity, String.class);
|
||||||
return getStringResponse(responseEntity);
|
return getStringResponse(responseEntity);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
return getErrorResponse(e);
|
||||||
if (e instanceof HttpClientErrorException) {
|
|
||||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
|
||||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
|
||||||
}
|
|
||||||
throw new RemoteRequestException(e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,11 +93,7 @@ public class RestRemoteRequest {
|
|||||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.DELETE, httpEntity, String.class);
|
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.DELETE, httpEntity, String.class);
|
||||||
return getStringResponse(responseEntity);
|
return getStringResponse(responseEntity);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (e instanceof HttpClientErrorException) {
|
return getErrorResponse(e);
|
||||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
|
||||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
|
||||||
}
|
|
||||||
throw new RemoteRequestException(e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,11 +110,7 @@ public class RestRemoteRequest {
|
|||||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.POST, httpEntity, String.class);
|
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.POST, httpEntity, String.class);
|
||||||
return getStringResponse(responseEntity);
|
return getStringResponse(responseEntity);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (e instanceof HttpClientErrorException) {
|
return getErrorResponse(e);
|
||||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
|
||||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
|
||||||
}
|
|
||||||
throw new RemoteRequestException(e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,11 +127,7 @@ public class RestRemoteRequest {
|
|||||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.POST, httpEntity, String.class);
|
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.POST, httpEntity, String.class);
|
||||||
return getStringResponse(responseEntity);
|
return getStringResponse(responseEntity);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (e instanceof HttpClientErrorException) {
|
return getErrorResponse(e);
|
||||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
|
||||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
|
||||||
}
|
|
||||||
throw new RemoteRequestException(e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,11 +144,7 @@ public class RestRemoteRequest {
|
|||||||
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.PUT, httpEntity, String.class);
|
ResponseEntity<String> responseEntity = restTemplate.exchange(remoteUri, HttpMethod.PUT, httpEntity, String.class);
|
||||||
return getStringResponse(responseEntity);
|
return getStringResponse(responseEntity);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (e instanceof HttpClientErrorException) {
|
return getErrorResponse(e);
|
||||||
HttpClientErrorException exception = (HttpClientErrorException) e;
|
|
||||||
throw new RemoteResponseException(exception.getResponseBodyAsString());
|
|
||||||
}
|
|
||||||
throw new RemoteRequestException(e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,11 +159,11 @@ public class RestRemoteRequest {
|
|||||||
HttpEntity<String> httpEntity;
|
HttpEntity<String> httpEntity;
|
||||||
if (jsonBody != null) {
|
if (jsonBody != null) {
|
||||||
HttpHeaders httpHeaders = getHttpHeaders(headerMap);
|
HttpHeaders httpHeaders = getHttpHeaders(headerMap);
|
||||||
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||||
if (jsonBody instanceof Collection) {
|
if (jsonBody instanceof Collection) {
|
||||||
httpEntity = new HttpEntity(JSONArray.toJSONString(jsonBody), getHttpHeaders(headerMap));
|
httpEntity = new HttpEntity(JSONArray.toJSONString(jsonBody), httpHeaders);
|
||||||
} else {
|
} else {
|
||||||
httpEntity = new HttpEntity(JSONObject.toJSONString(jsonBody), getHttpHeaders(headerMap));
|
httpEntity = new HttpEntity(JSONObject.toJSONString(jsonBody), httpHeaders);
|
||||||
}
|
}
|
||||||
} else if (formVariableParams != null) {
|
} else if (formVariableParams != null) {
|
||||||
HttpHeaders httpHeaders = getHttpHeaders(headerMap);
|
HttpHeaders httpHeaders = getHttpHeaders(headerMap);
|
||||||
@ -218,7 +201,30 @@ public class RestRemoteRequest {
|
|||||||
if (responseEntity.getStatusCode() != HttpStatus.OK) {
|
if (responseEntity.getStatusCode() != HttpStatus.OK) {
|
||||||
throw new RemoteResponseException("远程调用响应状态码不支持: " + responseEntity.getStatusCode());
|
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