- 定义接口类
/**
* api接口执行类
*/
public interface IApiExecuteService<T> {
/**
* @param baseApiConfig 请求参数
* @return T
* @description: 执行类
* @author qujiawen
* @date: 2023/2/22
*/
T execute(BaseApiConfig baseApiConfig);
}
2.定义容器工厂 ``
/**
* @author qujiawen
* @version 1.0
* @description: 容器工厂
* @date 2023/2/22
*/
@Service
public class ApiExecuteContext {
/**
* @description: 给对应类型的处理类装载进去
*/
@Autowired
Map<String, IApiExecuteService<?>> iApiExecuteServiceMap;
/**
* @param type 类型
* @return IApiExecuteService<?>
* @description: 根据类型获取到对于的处理类
* @author qujiawen
* @date: 2023/2/22
*/
public IApiExecuteService<?> getApiExecuteService(String type) {
IApiExecuteService<?> executeService = iApiExecuteServiceMap.get(type);
AssertUtils.notNull(executeService, "暂不支撑此类型");
return executeService;
}
}
- 定义具体实现类 API处理类
/**
* @description: API类型处理类
* @author qujiawen
* @date 2023/2/22
* @version 1.0
*/
//normalApi 作为 iApiExecuteServiceMap的key {"normalApi":"NormalApiServiceImpl"}
@Service("normalApi")
@Slf4j
public class NormalApiServiceImpl implements IApiExecuteService<Object> {
@Autowired
RestTemplate restTemplate;
@Override
public Object execute(BaseApiConfig baseApiConfig) {
log.info("进入标准api接口执行,baseApiConfig:{}", baseApiConfig);
HttpMethod httpMethod = HttpMethod.resolve(baseApiConfig.getRequestMethod().toUpperCase());
if (httpMethod == null) {
throw new ServiceException("请求方式不存在");
}
try {
HttpHeaders headers = new HttpHeaders();
String authorization = ServletUtils.getRequest().getHeader(HttpHeaders.AUTHORIZATION);
if (baseApiConfig.getApiHead() != null) {
Map<String, String> mapTypes = JSON.parseObject(baseApiConfig.getApiHead(), Map.class);
if (StrUtil.isEmpty(mapTypes.get(HttpHeaders.AUTHORIZATION))) {
mapTypes.put(HttpHeaders.AUTHORIZATION, authorization);
}
headers.setAll(mapTypes);
} else {
headers.add(HttpHeaders.AUTHORIZATION, authorization);
}
ResponseEntity<Object> response;
if (httpMethod.matches(HttpMethod.GET.name())) {
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
Map<String, ?> uriVariables = JSON.parseObject(baseApiConfig.getApiParams(), Map.class);
response = restTemplate.exchange(baseApiConfig.getApiUrl(), httpMethod, httpEntity, Object.class, uriVariables);
} else {
HttpEntity<String> httpEntity = new HttpEntity<>(baseApiConfig.getApiParams(), headers);
response = restTemplate.exchange(baseApiConfig.getApiUrl(), httpMethod, httpEntity, Object.class);
}
Object result;
if (response.getStatusCodeValue() == HttpStatus.SC_OK) {
result = response.getBody();
} else {
throw new ServiceException("调用触发接口返回结果异常:" + response.getBody());
}
return result;
} catch (Exception e) {
throw new ServiceException("调用触发接口失败:" + e.getMessage());
}
}
}
sql处理类
@Service("sqlApi")
@Slf4j
public class SqlApiServiceImpl implements IApiExecuteService<Object> {
@Resource
IDataLinkConfService dataLinkConfService;
@Override
public Object execute(BaseApiConfig baseApiConfig) {
log.info("进入sql接口执行,baseApiConfig:{}", baseApiConfig);
DataSourceDTO dataSource = dataLinkConfService.getDataSource(baseApiConfig.getConnIndex());
Map<String, ?> params = JSON.parseObject(baseApiConfig.getApiParams(), Map.class);
Object executeObj;
try {
if (StrUtil.startWithIgnoreCase(baseApiConfig.getApiSql(), "SELECT")) {
executeObj = dataSource.getDb().query(baseApiConfig.getApiSql(), params);
} else {
executeObj = dataSource.getDb().execute(baseApiConfig.getApiSql(), params);
}
log.info("sql:{} params:{} execute:{}", baseApiConfig.getApiSql(), params, executeObj);
} catch (SQLException exception) {
log.error("sql: {} params:{} :{} ", baseApiConfig.getApiSql(), params,exception);
throw new ServiceException("执行sql 失败:"+ exception.getMessage());
}
return executeObj;
}
}
- 调用
根据类型调用对应的处理方法