如何使用 Spring Integration WS 调用 RESTful API 和处理响应

How to invoke a RESTful API and proccess response with Spring Integration WS

假设我有另一个 Spring 带有这样控制器的引导项目:

@RestController
public class TestController {

    @GetMapping("/test")
    public String test(@RequestBody String body) {
        log.info("/test: {}", body);
        return "[CHANGED] " + body;
    }
}

我的目标是将消息从通道重定向到上面的端点。我该如何正确地做到这一点? 我发现 SI 有 Spring Integration Web Services 模块来处理这样的事情,但我无法制作一个工作示例。这是我的 IntegrationFlow:

public IntegrationFlow flow() {
    return IntegrationFlows.from(/* irrelevant */)
            .log(Object::toString)
            .handle(Ws.simpleOutboundGateway()
                    .uri("http://localhost:8080/test")
                    .requestCallback(message -> {
                        log.info("[REQUEST-CALLBACK] - Got response: {}", message);
                    })
            )
            .get();
}

应用程序无法启动并出现 java.lang.NoClassDefFoundError: javax/xml/soap/MessageFactory 错误,所以可能我没有为 WS 出站网关提供足够的参数。

我用谷歌搜索了这个错误,发现添加这个依赖项应该可以解决所有问题:

compile group: 'javax.xml.soap', name: 'javax.xml.soap-api', version: '1.4.0'

但事实并非如此。我不断收到关于丢失 类 的错误,只是不同的错误。

我的 dependecies 部分如下所示:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-json:2.4.0'
    implementation 'org.springframework.integration:spring-integration-core:5.4.1'
    implementation 'org.springframework.integration:spring-integration-ws:5.4.1'
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.11.3'
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.3'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

P.S。抱歉,如果答案是表面上的某个地方(互联网)。还在学习中,自己解决不了。

UPD:Java11

您可能没有正确阅读文档:https://docs.spring.io/spring-integration/docs/current/reference/html/ws.html#ws。 Spring Integration 的 WS 模块完全基于 Spring WS 项目,其目标正是关于 SOAP,而不是 REST。请阅读各自的规格以了解差异。

要调用 REST 服务,您需要 Spring 集成 HTTP 模块:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http