spring集成:如何从spring控制器调用spring集成?

5n0oy7gb  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(391)

拜托,你能帮我吗?
所有的来源都在这里。
(https://github.com/mcvzone/integration-tcp-test.git)
谢谢您。
1.我创建了一个spring集成tcp客户机上下文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.我创建了一个rest控制器。

@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引导并打开1234端口(newserversocket(1234))并调用url(http://localhost:8080/tcp/测试)
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]
kninwzqo

kninwzqo1#

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

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

注意安全 @RequestBody 在方法param上。默认情况下,springmvc不知道从请求Map到这个参数的参数中的内容。所以,它被保留为 null .
另一方面,当网关调用的参数为 null ,spring集成无法创建 Message<?> 发送,因为有效载荷不能 null . 因此你最终会有这样一个例外。
我们可能会修改这个异常消息,以便让最终用户更清楚地了解正在发生的事情。请随便提一个关于这件事的问题!

相关问题