新增@RemoteService的value处理,和@RemoteQueryMapParams注解
This commit is contained in:
parent
2c165fc97c
commit
01490c5f77
@ -1,5 +1,6 @@
|
||||
package ink.wgink.common.rpc.rest.handler;
|
||||
|
||||
import ink.wgink.annotation.rpc.rest.RemoteService;
|
||||
import ink.wgink.annotation.rpc.rest.method.RemoteDeleteMethod;
|
||||
import ink.wgink.annotation.rpc.rest.method.RemoteGetMethod;
|
||||
import ink.wgink.annotation.rpc.rest.method.RemotePostMethod;
|
||||
@ -52,10 +53,13 @@ public class RestRemoteHandler implements InvocationHandler {
|
||||
if (method.getParameters().length == 0) {
|
||||
throw new ParamsException("方法没有参数");
|
||||
}
|
||||
RemoteService remoteService = method.getDeclaringClass().getAnnotation(RemoteService.class);
|
||||
String baseUri = remoteService.value();
|
||||
String remotePath = getRemotePath(method.getParameters(), args);
|
||||
checkRemotePath(remotePath);
|
||||
Map<String, String> pathVariableParams = getPathVariableParams(method.getParameters(), args);
|
||||
Map<String, String> queryVariableParams = getQueryVariableParams(method.getParameters(), args);
|
||||
Map<String, String> queryMapParams = getQueryMapParams(method.getParameters(), args);
|
||||
Map<String, String> headerVariableParams = getHeaderVariableParams(method.getParameters(), args);
|
||||
MultiValueMap<String, Object> formVariableParams = null;
|
||||
String uri;
|
||||
@ -92,8 +96,8 @@ public class RestRemoteHandler implements InvocationHandler {
|
||||
List<String> pathVariable = listPathVariables(uri);
|
||||
checkPathVariables(pathVariable);
|
||||
checkPathVariableParams(pathVariable, pathVariableParams);
|
||||
uri = buildUri(uri, pathVariable, pathVariableParams);
|
||||
String remoteFullPath = buildFullUri(remotePath, uri, queryVariableParams);
|
||||
uri = buildUri(baseUri, uri, pathVariable, pathVariableParams);
|
||||
String remoteFullPath = buildFullUri(remotePath, uri, queryVariableParams, queryMapParams);
|
||||
RestRemoteRequest restRemoteRequest = new RestRemoteRequest();
|
||||
return restRemoteRequest.request(requestMethod, remoteFullPath, headerVariableParams, formVariableParams, requestBody, method.getGenericReturnType());
|
||||
}
|
||||
@ -246,6 +250,37 @@ public class RestRemoteHandler implements InvocationHandler {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询Map参数
|
||||
*
|
||||
* @param parameters
|
||||
* @param args
|
||||
* @return
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
private Map<String, String> getQueryMapParams(Parameter[] parameters, Object[] args) throws UnsupportedEncodingException {
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
Parameter parameter = parameters[i];
|
||||
if (!parameter.isAnnotationPresent(RemoteQueryParamsMap.class)) {
|
||||
continue;
|
||||
}
|
||||
if (parameter.getType() != Map.class) {
|
||||
continue;
|
||||
}
|
||||
if (args[i] == null) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> argMap = (Map<String, Object>) args[i];
|
||||
Map<String, String> queryMap = new HashMap<>();
|
||||
for (Map.Entry<String, Object> kv : argMap.entrySet()) {
|
||||
queryMap.put(kv.getKey(), URLEncoder.encode(kv.getValue().toString(), "UTF-8"));
|
||||
}
|
||||
return queryMap;
|
||||
}
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取远程地址,只取第一个
|
||||
*
|
||||
@ -328,12 +363,19 @@ public class RestRemoteHandler implements InvocationHandler {
|
||||
* @param pathVariableParams
|
||||
* @return
|
||||
*/
|
||||
private String buildUri(String uri, List<String> pathVariables, Map<String, String> pathVariableParams) {
|
||||
private String buildUri(String baseUri, String uri, List<String> pathVariables, Map<String, String> pathVariableParams) {
|
||||
String resultBaseUri = baseUri;
|
||||
if (!StringUtils.isBlank(baseUri)) {
|
||||
for (String pathVariable : pathVariables) {
|
||||
resultBaseUri = resultBaseUri.replace("{" + pathVariable + "}", pathVariableParams.get(pathVariable));
|
||||
}
|
||||
resultBaseUri = resultBaseUri.startsWith("/") ? resultBaseUri : "/" + resultBaseUri;
|
||||
}
|
||||
String resultUri = uri;
|
||||
for (String pathVariable : pathVariables) {
|
||||
resultUri = resultUri.replace("{" + pathVariable + "}", pathVariableParams.get(pathVariable));
|
||||
}
|
||||
return resultUri.startsWith("/") ? resultUri : "/" + resultUri;
|
||||
return resultBaseUri + (resultUri.startsWith("/") ? resultUri : "/" + resultUri);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -342,12 +384,13 @@ public class RestRemoteHandler implements InvocationHandler {
|
||||
* @param remotePath 远程调用地址
|
||||
* @param uri 请求路径
|
||||
* @param queryVariableParams 查询参数
|
||||
* @param queryMapParams 查询Map参数
|
||||
* @return
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
private String buildFullUri(String remotePath, String uri, Map<String, String> queryVariableParams) {
|
||||
private String buildFullUri(String remotePath, String uri, Map<String, String> queryVariableParams, Map<String, String> queryMapParams) {
|
||||
String remote = remotePath.endsWith("/") ? remotePath.substring(0, remotePath.length() - 1) : remotePath;
|
||||
if (queryVariableParams.isEmpty()) {
|
||||
if (queryVariableParams.isEmpty() && queryMapParams.isEmpty()) {
|
||||
return remotePath + uri;
|
||||
}
|
||||
StringBuilder queryParams = new StringBuilder();
|
||||
@ -357,6 +400,12 @@ public class RestRemoteHandler implements InvocationHandler {
|
||||
}
|
||||
queryParams.append(kv.getKey()).append("=").append(kv.getValue());
|
||||
}
|
||||
for (Map.Entry<String, String> kv : queryVariableParams.entrySet()) {
|
||||
if (queryParams.length() > 0) {
|
||||
queryParams.append("&");
|
||||
}
|
||||
queryParams.append(kv.getKey()).append("=").append(kv.getValue());
|
||||
}
|
||||
return remote + uri + "?" + queryParams;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user