能否将Enterprise Integrator中创建的REST API接入API Manager?
将Enterprise Integrator中的REST API接入API Manager的实现方案
一、从Integration Studio导入API到API Manager
- 打开Integration Studio,找到你的
DBLookupAPI项目 - 右键点击API文件,选择Export -> Export to API Manager
- 按向导填写API Manager连接信息(服务器地址、端口、管理员账号)
- 确认API基础信息(名称、上下文路径
/dblookup、资源路径/{UserCode}、HTTP方法GET)后完成导入
二、手动在API Manager配置API(替代导入方式)
如果不通过Integration Studio导入,可直接在API Manager中创建:
- 登录API Manager Publisher界面
- 点击Create API -> Design New API
- 填写API基本信息:
- 名称:
DBLookupAPI - 上下文:
/dblookup - 版本:按需设置(如v1)
- 名称:
- 添加资源:
- HTTP方法:
GET - URI模板:
/{UserCode}
- HTTP方法:
- 配置端点:指向EI中该API的部署地址(例如
http://<EI_HOST>:8280/dblookup)
三、API Manager关键配置(保障C#应用可调用)
- API订阅:在API Manager Store界面创建应用,订阅
DBLookupAPI并获取访问令牌(API Key/OAuth2令牌,依认证方式选择) - CORS开启:若C#应用为跨域调用,在API Publisher的API配置中开启CORS,允许对应Origin、Headers和Methods
- 认证配置:根据需求选择认证方式(如API Key、OAuth2),确保C#应用能正确携带认证信息
四、你的API代码优化与注意事项
以下是调整后的API代码,修正了潜在问题:
<api xmlns="http://ws.apache.org/ns/synapse" name="DBLookupAPI" context="/dblookup"> <resource methods="GET" uri-template="/{UserCode}"> <inSequence> <log level="custom"> <property name="Value" expression="get-property('uri.var.UserCode')"/> </log> <dblookup> <connection> <pool> <driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver> <url>jdbc:sqlserver://10.1.1.111\test;databaseName=UserDB</url> <user>admin</user> <password>admin</password> </pool> </connection> <statement> <sql>select UserCode from UserDB.dbo.Users where UserCode =?;</sql> <!-- 修正:移除表达式中UserCode后的多余空格 --> <parameter expression="get-property('uri.var.UserCode')" type="CHAR"/> <!-- 修正:移除结果列名后的多余空格 --> <result name="foundnr" column="UserCode"/> </statement> </dblookup> <log level="custom"> <property name="Value" expression="get-property('foundnr')"/> </log> <filter source="boolean(get-property('foundnr'))" regex="true"> <then> <log> <property name="Message" value="用户存在,执行重定向"/> </log> <property name="HTTP_SC" value="302"/> <!-- 需确保EI中已配置Redirect环境变量 --> <property name="Location" expression="get-property('env', 'Redirect')" scope="transport"/> </then> <else> <log> <property name="Message" value="用户不存在,执行重定向"/> </log> <property name="HTTP_SC" value="302"/> <!-- 需确保EI中已配置Redirect2环境变量 --> <property name="Location" expression="get-property('env', 'Redirect2')" scope="transport"/> </else> </filter> <respond/> </inSequence> <outSequence/> <faultSequence/> </resource> </api>
- 修正了
dblookup模块中参数表达式和结果列名的空格问题,避免查询失败 - 需在EI中提前配置
Redirect和Redirect2环境变量,否则重定向地址会为空 - 数据库连接信息建议通过EI的
deployment.toml配置文件管理,避免硬编码账号密码
五、C#应用调用示例
使用HttpClient调用API Manager发布后的API:
using System; using System.Net.Http; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { string apiUrl = "http://<API_MANAGER_HOST>:8243/dblookup/v1/TESTUSER"; string apiKey = "YOUR_API_KEY_FROM_API_MANAGER"; using (HttpClient client = new HttpClient()) { // 添加API Key认证头(依API Manager配置的认证方式调整) client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); HttpResponseMessage response = await client.GetAsync(apiUrl); // 处理重定向逻辑 if (response.StatusCode == System.Net.HttpStatusCode.Redirect) { string redirectUrl = response.Headers.Location.ToString(); Console.WriteLine($"重定向地址:{redirectUrl}"); // 如需自动跟随重定向,可添加:client.AllowAutoRedirect = true; } else { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } } }
- 替换
<API_MANAGER_HOST>为你的API Manager地址,YOUR_API_KEY_FROM_API_MANAGER为订阅后获取的API Key - 若API Manager使用OAuth2认证,需先获取访问令牌,再携带
Authorization: Bearer <TOKEN>请求头
内容的提问来源于stack exchange,提问作者Kelisha Govender
相关产品推荐
相关产品推荐

