Spring 集成 - 如何以编程方式使用 uri 参数/url 表达式?
Spring Integration - How to use uri param / url expression programmatically?
我必须为以下步骤编写 Spring 集成流程:
(1) 从目录json 中读取文件
(2) 变换为Java object 加header
(3) 转换为JSON
(4)Post到终点:POSThttp://127.0.0.1:8081/v1/userValidation
(5) 提取对名为 'UserValidationResponse' 的 Java Object 的响应。响应有一个名为 orderID 的字段,将在步骤 7 中使用。
(6) 将输出写入名为 'userValidationPostOutputWriterChannel' 的通道。此频道将记录 Http 状态。
(7) PUT 在终点:PUT http://127.0.0.1:8081/v1/userValidation/{第5步提取的orderID}/cancel.
(8) 将输出写入名为 'userValidationPostCancelOutputWriterChannel' 的通道。此频道将记录 Http 状态。
我需要什么:
如何动态构造URLhttp://127.0.0.1:8081/v1/userValidation/{orderID}/cancel?
网上查了一下,发现必须要用uri参数。
你能以编程方式给我一个例子吗?
我只能找到使用配置 XML 的示例,而我的代码中没有配置 XML。
示例代码:
@Bean
public IntegrationFlow placeUserRequest(RestTemplate restTemplate) {
private String AUTHORIZATION = "USER";
return IntegrationFlows.from(sourceDirectory(), configurer -> configurer.poller(Pollers.fixedDelay(10000)))
.transform(Transformers.fromJson(UserRequest.class))
.enrichHeaders(h -> h.header("Content-Type", "application/json"))
.transform(Transformers.toJson())
.handle(Http.outboundGateway("http://127.0.0.1:8081/v1/userValidation", restTemplate)
.httpMethod(HttpMethod.POST)
.mappedRequestHeaders("auth*", "TraceabilityID", AUTHORIZATION)
.charset("UTF-8")
.expectedResponseType(UserValidationResponse.class))
.wireTap(flow -> flow.channel("userValidationPostOutputWriterChannel"))
.handle(Http.outboundGateway("http://127.0.0.1:8081/v1/userValidation/" + parser.parseExpression("payload.getOrderID()") + "/cancel", restTemplate)
.httpMethod(HttpMethod.PUT)
.mappedRequestHeaders("auth*", "TraceabilityID", AUTHORIZATION)
.charset("UTF-8").expectedResponseType(Map.class))
.wireTap(flow -> flow.channel("userValidationPostCancelOutputWriterChannel"))
.get();
}
来自 POST http://127.0.0.1:8081/v1/userValidation 的示例响应:
{
"status": "CREATED",
"orderID": "78e323f7-d3f9-11e9-a71a-035a2a38a4e0",
"userData" : {
"userFName": "John",
"userLName": "Smith",
"userLocation": "Florida"
},
"userAccess": "USER"
}
下一次执行应该是 PUT http://127.0.0.1:8081/v1/userValidation/78e323f7-d3f9-11e9-a71a-035a2a38a4e0/cancel。
像这样配置 HTTP 出站网关上的 URI 是完全可以的:
http://127.0.0.1:8081/v1/userValidation/{orderID}/cancel
这叫做template
。你有 orderID
uriVariable。 Http.outboundGateway
也支持:
/**
* Specify a value SpEL expression for the uri template variable.
* @param variable the uri template variable.
* @param expression the expression to evaluate value for te uri template variable.
* @return the current Spec.
* @see AbstractHttpRequestExecutingMessageHandler#setUriVariableExpressions(Map)
*/
public S uriVariable(String variable, String expression) {
因此,对于您的用例,它必须是这样的:
.handle(Http.outboundGateway("http://127.0.0.1:8081/v1/userValidation/{orderID}/cancel", restTemplate)
.httpMethod(HttpMethod.PUT)
.mappedRequestHeaders("auth*", "TraceabilityID", AUTHORIZATION)
.charset("UTF-8").expectedResponseType(Map.class)
.uriVariable("orderID", "payload.orderID"))
我必须为以下步骤编写 Spring 集成流程:
(1) 从目录json 中读取文件
(2) 变换为Java object 加header
(3) 转换为JSON
(4)Post到终点:POSThttp://127.0.0.1:8081/v1/userValidation
(5) 提取对名为 'UserValidationResponse' 的 Java Object 的响应。响应有一个名为 orderID 的字段,将在步骤 7 中使用。
(6) 将输出写入名为 'userValidationPostOutputWriterChannel' 的通道。此频道将记录 Http 状态。
(7) PUT 在终点:PUT http://127.0.0.1:8081/v1/userValidation/{第5步提取的orderID}/cancel.
(8) 将输出写入名为 'userValidationPostCancelOutputWriterChannel' 的通道。此频道将记录 Http 状态。
我需要什么:
如何动态构造URLhttp://127.0.0.1:8081/v1/userValidation/{orderID}/cancel?
网上查了一下,发现必须要用uri参数。
你能以编程方式给我一个例子吗?
我只能找到使用配置 XML 的示例,而我的代码中没有配置 XML。
示例代码:
@Bean
public IntegrationFlow placeUserRequest(RestTemplate restTemplate) {
private String AUTHORIZATION = "USER";
return IntegrationFlows.from(sourceDirectory(), configurer -> configurer.poller(Pollers.fixedDelay(10000)))
.transform(Transformers.fromJson(UserRequest.class))
.enrichHeaders(h -> h.header("Content-Type", "application/json"))
.transform(Transformers.toJson())
.handle(Http.outboundGateway("http://127.0.0.1:8081/v1/userValidation", restTemplate)
.httpMethod(HttpMethod.POST)
.mappedRequestHeaders("auth*", "TraceabilityID", AUTHORIZATION)
.charset("UTF-8")
.expectedResponseType(UserValidationResponse.class))
.wireTap(flow -> flow.channel("userValidationPostOutputWriterChannel"))
.handle(Http.outboundGateway("http://127.0.0.1:8081/v1/userValidation/" + parser.parseExpression("payload.getOrderID()") + "/cancel", restTemplate)
.httpMethod(HttpMethod.PUT)
.mappedRequestHeaders("auth*", "TraceabilityID", AUTHORIZATION)
.charset("UTF-8").expectedResponseType(Map.class))
.wireTap(flow -> flow.channel("userValidationPostCancelOutputWriterChannel"))
.get();
}
来自 POST http://127.0.0.1:8081/v1/userValidation 的示例响应:
{
"status": "CREATED",
"orderID": "78e323f7-d3f9-11e9-a71a-035a2a38a4e0",
"userData" : {
"userFName": "John",
"userLName": "Smith",
"userLocation": "Florida"
},
"userAccess": "USER"
}
下一次执行应该是 PUT http://127.0.0.1:8081/v1/userValidation/78e323f7-d3f9-11e9-a71a-035a2a38a4e0/cancel。
像这样配置 HTTP 出站网关上的 URI 是完全可以的:
http://127.0.0.1:8081/v1/userValidation/{orderID}/cancel
这叫做template
。你有 orderID
uriVariable。 Http.outboundGateway
也支持:
/**
* Specify a value SpEL expression for the uri template variable.
* @param variable the uri template variable.
* @param expression the expression to evaluate value for te uri template variable.
* @return the current Spec.
* @see AbstractHttpRequestExecutingMessageHandler#setUriVariableExpressions(Map)
*/
public S uriVariable(String variable, String expression) {
因此,对于您的用例,它必须是这样的:
.handle(Http.outboundGateway("http://127.0.0.1:8081/v1/userValidation/{orderID}/cancel", restTemplate)
.httpMethod(HttpMethod.PUT)
.mappedRequestHeaders("auth*", "TraceabilityID", AUTHORIZATION)
.charset("UTF-8").expectedResponseType(Map.class)
.uriVariable("orderID", "payload.orderID"))