有没有办法调用变压器方法来更改春季集成流中的有效载荷

发布于 2025-02-08 18:48:23 字数 1591 浏览 2 评论 0原文

在以下代码中,我将有效载荷转换为其他有效载荷,并将其作为请求主体发送到API呼叫。我想通过外部方法调用将转换外包。有可能吗?

    @Bean
            public IntegrationFlow flow3(){
                return integrationFlowDefinition -> integrationFlowDefinition
                        .channel(c -> c.executor(Executors.newCachedThreadPool())).log()
//                        .split("payload.employee")
//                        .transform(Transformers.toJson()).log()
                        .transform(Transformers.fromJson(Map.class)).log("json payload to Map object")
                        .<Map<String, String>, Map<String,String>>transform(
                                payload -> {
                                    payload.put("name","Somnath Mukhopadhyay");
                                    payload.put("company","xyz");
//                                    payload.put("salary", "20000");
                                    return payload;
                                }
                        ).log("Modifying the payload")
                        .transform(Transformers.toJson()).log("modified Map object to JSON")
                        .enrichHeaders(headerEnricherSpec -> headerEnricherSpec.header("ContentType","application/json"))
                        .handle(Http.outboundGateway("http://localhost:8888/Employee")
                                .httpMethod(HttpMethod.POST)
                                .expectedResponseType(String.class)
                                )
                        .log("Getting response back from flow3");
            }

In the below code I'm transforming the payload to a different payload and sending it as a request body to a POST API call. I want to outsource that transformation through a external method call. Is that possible?

    @Bean
            public IntegrationFlow flow3(){
                return integrationFlowDefinition -> integrationFlowDefinition
                        .channel(c -> c.executor(Executors.newCachedThreadPool())).log()
//                        .split("payload.employee")
//                        .transform(Transformers.toJson()).log()
                        .transform(Transformers.fromJson(Map.class)).log("json payload to Map object")
                        .<Map<String, String>, Map<String,String>>transform(
                                payload -> {
                                    payload.put("name","Somnath Mukhopadhyay");
                                    payload.put("company","xyz");
//                                    payload.put("salary", "20000");
                                    return payload;
                                }
                        ).log("Modifying the payload")
                        .transform(Transformers.toJson()).log("modified Map object to JSON")
                        .enrichHeaders(headerEnricherSpec -> headerEnricherSpec.header("ContentType","application/json"))
                        .handle(Http.outboundGateway("http://localhost:8888/Employee")
                                .httpMethod(HttpMethod.POST)
                                .expectedResponseType(String.class)
                                )
                        .log("Getting response back from flow3");
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2025-02-15 18:48:23

有一个适合您的:

/**

 * Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}

 * to invoke the service method at runtime.

 * @param service the service to use.

 * @param methodName the method to invoke.

 * @return the current {@link BaseIntegrationFlowDefinition}.

 * @see MethodInvokingTransformer

 */

public B transform(Object service, String methodName) {

阅读这些DSL操作员的Javadocs。

There is this one for you:

/**

 * Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}

 * to invoke the service method at runtime.

 * @param service the service to use.

 * @param methodName the method to invoke.

 * @return the current {@link BaseIntegrationFlowDefinition}.

 * @see MethodInvokingTransformer

 */

public B transform(Object service, String methodName) {

Read javadocs for those DSL operators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文