【问题标题】:spring integration: How do I call the Spring Integration from Spring Controller?spring integration:如何从 Spring Controller 调用 Spring Integration?
【发布时间】:2021-05-29 12:21:33
【问题描述】:

拜托,你能帮帮我吗?
所有来源都在这里。
(https://github.com/mcvzone/integration-tcp-test.git)

谢谢。

1.我创建了一个 spring integration-tcp-client 上下文 xml 文件。

    <int:gateway id="gw"
                 service-interface="com.example.demo.module.SimpleGateway"
                 default-request-channel="input"/>

    <int-ip:tcp-connection-factory id="client"
                                   type="client"
                                   host="localhost"
                                   port="1234"
                                   single-use="true"
                                   so-timeout="10000"/>

    <int:channel id="input"/>

    <int-ip:tcp-outbound-gateway id="outGateway"
                                 request-channel="input"
                                 reply-channel="clientBytes2StringChannel"
                                 connection-factory="client"
                                 request-timeout="10000"
                                 reply-timeout="10000"/>

    <int:object-to-string-transformer id="clientBytes2String"
                                      input-channel="clientBytes2StringChannel"/>

2。我创建了一个 RestController。

@RestController
public class TcpController {
    
    final GenericXmlApplicationContext context;
    final SimpleGateway simpleGateway;
    
    public TcpController(){
        this.context = new GenericXmlApplicationContext();
        context.load("classpath:META-INF/spring/integration/tcpClientServerDemo-context.xml");
        context.registerShutdownHook();
        context.refresh();
        
        this.simpleGateway = context.getBean(SimpleGateway.class);
    }
    
    @RequestMapping("/tcp/test")
    public String test(String name) {
        //SimpleGateway simpleGateway = context.getBean(SimpleGateway.class);
        String result = simpleGateway.send(name);
        System.out.println("result : " + result);
        return result;
    }

}

3.我启动 spring boot 并打开 1234 端口 (new ServerSocket(1234)) 并调用 url。(http://localhost:8080/tcp/test)
4。结果是错误。

java.lang.IllegalArgumentException: unable to determine a Message or payload parameter on method
.
.
.
at com.sun.proxy.$Proxy60.send(Unknown Source) ~[na:na]
at com.example.demo.TcpController.test(TcpController.java:25) ~[classes/:na]

【问题讨论】:

  • 你能显示更多的堆栈跟踪吗?
  • 同时显示SimpleGateway 代码。
  • 查看我的回答并附上一些解释。

标签: spring tcp spring-integration integration


【解决方案1】:

当我将您的代码更改为以下代码时,它已经开始工作了:

@RequestMapping("/tcp/test")
public String test(@RequestBody String name) {

注意方法参数上的@RequestBody。默认情况下,Spring MVC 不知道从请求中映射到此参数的参数的内容。所以,留下null

另一方面,当该网关调用的参数是 null 时,Spring Integration 无法创建要发送的 Message&lt;?&gt;,因为有效负载不能是 null。因此,您最终会遇到这样的异常。

我们可能会修改该异常消息,以使最终用户更清楚正在发生的事情。随时就此事提出 GH 问题!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多