修复MagicAPIService.execute/call方法需要携带magic-api.prefix的问题
This commit is contained in:
parent
df32a94cde
commit
6e6f05eae2
@ -252,7 +252,7 @@ public class MagicAPIAutoConfiguration implements WebMvcConfigurer, WebSocketCon
|
|||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public MagicAPIService magicAPIService(ResultProvider resultProvider, MagicResourceService magicResourceService, RequestMagicDynamicRegistry requestMagicDynamicRegistry, FunctionMagicDynamicRegistry functionMagicDynamicRegistry) {
|
public MagicAPIService magicAPIService(ResultProvider resultProvider, MagicResourceService magicResourceService, RequestMagicDynamicRegistry requestMagicDynamicRegistry, FunctionMagicDynamicRegistry functionMagicDynamicRegistry) {
|
||||||
return new DefaultMagicAPIService(resultProvider, properties.getInstanceId(), magicResourceService, requestMagicDynamicRegistry, functionMagicDynamicRegistry, properties.isThrowException(), applicationContext);
|
return new DefaultMagicAPIService(resultProvider, properties.getInstanceId(), magicResourceService, requestMagicDynamicRegistry, functionMagicDynamicRegistry, properties.isThrowException(), properties.getPrefix() ,applicationContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package org.ssssssss.magicapi.core.service.impl;
|
package org.ssssssss.magicapi.core.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
@ -10,6 +11,9 @@ import org.springframework.http.MediaType;
|
|||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.context.request.RequestAttributes;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
import org.ssssssss.magicapi.core.annotation.MagicModule;
|
import org.ssssssss.magicapi.core.annotation.MagicModule;
|
||||||
import org.ssssssss.magicapi.core.config.Constants;
|
import org.ssssssss.magicapi.core.config.Constants;
|
||||||
import org.ssssssss.magicapi.core.config.JsonCodeConstants;
|
import org.ssssssss.magicapi.core.config.JsonCodeConstants;
|
||||||
@ -47,6 +51,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
|||||||
private final ApplicationEventPublisher publisher;
|
private final ApplicationEventPublisher publisher;
|
||||||
private final RequestMagicDynamicRegistry requestMagicDynamicRegistry;
|
private final RequestMagicDynamicRegistry requestMagicDynamicRegistry;
|
||||||
private final FunctionMagicDynamicRegistry functionMagicDynamicRegistry;
|
private final FunctionMagicDynamicRegistry functionMagicDynamicRegistry;
|
||||||
|
private final String prefix;
|
||||||
|
|
||||||
public DefaultMagicAPIService(ResultProvider resultProvider,
|
public DefaultMagicAPIService(ResultProvider resultProvider,
|
||||||
String instanceId,
|
String instanceId,
|
||||||
@ -54,6 +59,7 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
|||||||
RequestMagicDynamicRegistry requestMagicDynamicRegistry,
|
RequestMagicDynamicRegistry requestMagicDynamicRegistry,
|
||||||
FunctionMagicDynamicRegistry functionMagicDynamicRegistry,
|
FunctionMagicDynamicRegistry functionMagicDynamicRegistry,
|
||||||
boolean throwException,
|
boolean throwException,
|
||||||
|
String prefix,
|
||||||
ApplicationEventPublisher publisher) {
|
ApplicationEventPublisher publisher) {
|
||||||
this.resultProvider = resultProvider;
|
this.resultProvider = resultProvider;
|
||||||
this.requestMagicDynamicRegistry = requestMagicDynamicRegistry;
|
this.requestMagicDynamicRegistry = requestMagicDynamicRegistry;
|
||||||
@ -61,10 +67,12 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
|||||||
this.throwException = throwException;
|
this.throwException = throwException;
|
||||||
this.resourceService = resourceService;
|
this.resourceService = resourceService;
|
||||||
this.instanceId = instanceId;
|
this.instanceId = instanceId;
|
||||||
|
this.prefix = StringUtils.defaultIfBlank(prefix, "");
|
||||||
this.publisher = publisher;
|
this.publisher = publisher;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object execute(PathMagicEntity info, Map<String, Object> context) {
|
@SuppressWarnings({"unchecked"})
|
||||||
|
private <T> T execute(RequestEntity requestEntity, PathMagicEntity info, Map<String, Object> context) {
|
||||||
|
|
||||||
MagicScriptContext scriptContext = new MagicScriptContext();
|
MagicScriptContext scriptContext = new MagicScriptContext();
|
||||||
String fullGroupName = resourceService.getGroupName(info.getGroupId());
|
String fullGroupName = resourceService.getGroupName(info.getGroupId());
|
||||||
@ -72,26 +80,38 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
|||||||
String scriptName = PathUtils.replaceSlash(String.format("/%s/%s(/%s/%s)", fullGroupName, info.getName(), fullGroupPath, info.getPath()));
|
String scriptName = PathUtils.replaceSlash(String.format("/%s/%s(/%s/%s)", fullGroupName, info.getName(), fullGroupPath, info.getPath()));
|
||||||
scriptContext.setScriptName(scriptName);
|
scriptContext.setScriptName(scriptName);
|
||||||
scriptContext.putMapIntoContext(context);
|
scriptContext.putMapIntoContext(context);
|
||||||
return ScriptManager.executeScript(info.getScript(), scriptContext);
|
if(requestEntity != null){
|
||||||
|
requestEntity.setMagicScriptContext(scriptContext);
|
||||||
|
}
|
||||||
|
return (T) ScriptManager.executeScript(info.getScript(), scriptContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T execute(String method, String path, Map<String, Object> context) {
|
public <T> T execute(String method, String path, Map<String, Object> context) {
|
||||||
String mappingKey = Objects.toString(method, "GET").toUpperCase() + ":" + PathUtils.replaceSlash("/" + Objects.toString(path, ""));
|
return execute(null, method, path, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T execute(RequestEntity requestEntity, String method, String path, Map<String, Object> context){
|
||||||
|
String mappingKey = Objects.toString(method, "GET").toUpperCase() + ":" + PathUtils.replaceSlash(this.prefix + "/" + Objects.toString(path, ""));
|
||||||
ApiInfo info = requestMagicDynamicRegistry.getMapping(mappingKey);
|
ApiInfo info = requestMagicDynamicRegistry.getMapping(mappingKey);
|
||||||
if (info == null) {
|
if (info == null) {
|
||||||
throw new MagicAPIException(String.format("找不到对应接口 [%s:%s]", method, path));
|
throw new MagicAPIException(String.format("找不到对应接口 [%s:%s]", method, path));
|
||||||
}
|
}
|
||||||
return (T) execute(info, context);
|
context.put("apiInfo", info);
|
||||||
|
return execute(requestEntity, info, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"unchecked"})
|
||||||
@Override
|
@Override
|
||||||
public <T> T call(String method, String path, Map<String, Object> context) {
|
public <T> T call(String method, String path, Map<String, Object> context) {
|
||||||
RequestEntity requestEntity = RequestEntity.create();
|
RequestEntity requestEntity = RequestEntity.create();
|
||||||
try {
|
try {
|
||||||
return (T) resultProvider.buildResult(requestEntity, (Object) execute(method, path, context));
|
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||||
} catch (MagicResourceNotFoundException e) {
|
if (requestAttributes instanceof ServletRequestAttributes) {
|
||||||
return null; //找不到对应接口
|
requestEntity.request(((ServletRequestAttributes) requestAttributes).getRequest())
|
||||||
|
.response(((ServletRequestAttributes) requestAttributes).getResponse());
|
||||||
|
}
|
||||||
|
return (T) resultProvider.buildResult(requestEntity, (Object) execute(requestEntity, method, path, context));
|
||||||
} catch (Throwable root) {
|
} catch (Throwable root) {
|
||||||
if (throwException) {
|
if (throwException) {
|
||||||
throw root;
|
throw root;
|
||||||
@ -100,13 +120,14 @@ public class DefaultMagicAPIService implements MagicAPIService, JsonCodeConstant
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"unchecked"})
|
||||||
@Override
|
@Override
|
||||||
public <T> T invoke(String path, Map<String, Object> context) {
|
public <T> T invoke(String path, Map<String, Object> context) {
|
||||||
FunctionInfo functionInfo = functionMagicDynamicRegistry.getMapping(path);
|
FunctionInfo functionInfo = functionMagicDynamicRegistry.getMapping(path);
|
||||||
if (functionInfo == null) {
|
if (functionInfo == null) {
|
||||||
throw new MagicAPIException(String.format("找不到对应函数 [%s]", path));
|
throw new MagicAPIException(String.format("找不到对应函数 [%s]", path));
|
||||||
}
|
}
|
||||||
return (T) execute(functionInfo, context);
|
return (T) execute(null, functionInfo, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user