mule - 如何在 mule 中设置查询参数以服务端点请求
问题描述
我有一个服务端点 http://xyzhost:port/greet123 ,它接受请求参数 salary和age,我需要将查询参数作为请求的一部分传递,例如 http://xyzhost:port/greet123?salary=100000&age=32 。我是 mule 新手,我成功调用了其他没有请求参数的服务端点,但是当有要传递的查询参数时,我无法设置查询参数并获得正确的响应。我正在使用 mule 3.3 版本。下面是我的 mule 流程。在使用http:outbound-endpoint到达服务端点之前在 mule 流中我正在使用自定义转换器(自定义请求转换器)将查询参数设置为出站请求范围。但是没有运气,由于我收到 400 - Bad request 作为来自服务端点的响应,因此无法将查询参数设置为请求的一部分。甚至我也尝试过使用在骡流中。谁能建议我如何将查询参数设置为服务端点。
下面是 mule flow xml 文件和自定义请求转换器
<http:connector name="ClientOutboundHttpConnector">
<service-overrides sessionHandler="org.mule.session.NullSessionHandler"/>
</http:connector>
<!-- Below is the flow for sample rest endpoint -->
<custom-transformer
name="SampleRequestTransformer"
class="com.example.SampleRequestTrasformer" />
<custom-transformer
name="SampleResponseTransformer"
class="com.example.SampleResponseTrasformer" />
<flow name="sampleRESTFlow" doc:name="sample rest Flow">
<inbound-endpoint address="servlet://getDetails" exchange-pattern="request-response" connector-ref="muleCXFServletConnector"
transformer-refs="SampleRequestTransformer" responseTimeout="600000" />
<http:outbound-endpoint
address="http://xyzhost:port/greet123"
connector-ref="ClientOutboundHttpConnector"
exchange-pattern="request-response"
contentType="application/x-www-form-urlencoded"
keep-alive="true"
responseTimeout="360000"
responseTransformer-refs="InputStreamToStringTransformer"/>
<logger
message="MappingServer Response after reading stream=#[payload]"
level="INFO" />
<transformer ref="SampleResponseTransformer" />
<transformer ref="clientErrorResponseTransformer" />
</flow>
下面是自定义请求转换器的代码
@Override
public Object transformMessage(MuleMessage message, String arg1)
throws TransformerException {
// TODO Auto-generated method stub
Object payload = message.getPayload();
Map<String,String> queryParams= new HashMap<String,String>();
queryParams.put("salary", "400000");
queryParams.put("age", "32");
message.setProperty("Content-Type", "application/x-www-form-urlencoded", PropertyScope.OUTBOUND);
message.setProperty("mimeType","application/x-www-form-urlencoded",PropertyScope.OUTBOUND);
message.setProperty("http.method", "POST", PropertyScope.OUTBOUND);
message.setProperty("http.query.params",queryParams, PropertyScope.OUTBOUND);
return message;
}
}```
解决方案
首先 - http 错误 400 表示资源不可用。但是服务器和端口都很好。因此,/greet123 不可用。问题不在于您的代码 - 问题在于没有人在听它。尝试常规浏览器或 Postman - 结果将相同 - 400 错误。
第二 - Mule 3 已弃用。据我所知,今年 3.8 将不再受支持。3.3 好久不见了。Mule 4 是当前版本,Mule 3 无法转移到 Mule 4。
https://simpleflatservice.com/mule4/Mule3toMule4Transformation.html
推荐阅读
- git - 将新提交的更改应用到旧提交,而无需中间提交
- python - 派格莱特。如何动态更改顶点的图片(动画)。OpenGL
- ios - Swift:TableView(单元格内)未加载单元格或 cellForRowAt 内的代码
- python - Python - 使用格式/f字符串输出带有0填充和中心的十六进制
- r - 在 R 中设置缺少的日期时间
- sql - 在 SQL 中的以下段上完成交叉表的正确方法是什么?
- scala - 如何检查一个值是否是 Scala 中的 unix 时间戳?
- c# - Azure 上的文件保存路径
- r - 通过对方程求微分来计算 OLS 估计量
- android - 如何避免基于 UI 语言拆分字符串