简体   繁体   English

从Spring Integration流程调用Spring Controller

[英]Invoke Spring Controller from Spring Integration flow

Hi I have a little problem. 嗨,我有一个小问题。 I want to invoke spring controller manually but I have an exception. 我想手动调用spring控制器,但是有一个例外。 Firstly, let me show you some integration flow and controller: 首先,让我向您展示一些集成流程和控制器:

@Bean
public IntegrationFlow flow() {
    return IntegrationFlows.from(
            Amqp.inboundAdapter(rabbitMqConfig.connectionFactory(), queue)
                    .acknowledgeMode(AcknowledgeMode.MANUAL)
                    .errorChannel("errorChannel")
                    .concurrentConsumers(2)
                    .maxConcurrentConsumers(3))
            .transform(Transformers.fromJson(Event.class))
            .transform(new EventToRequestTransformer())
            .handle(Request.class, (request, headers) -> controller.trigger(request))
            .<ResponseEntity, HttpStatus>transform(ResponseEntity::getStatusCode)
            .routeToRecipients(some routing)
            .get();
}


@Controller
public class SomeController {

    @RequestMapping(value = "/trigger", method = RequestMethod.POST)
    public ResponseEntity<Response> trigger(@RequestBody Request request) 
    {
        //some logic
    }
}

When I'm running my app and sending an event I am getting exception on line: 当我运行我的应用程序并发送事件时,我在网上遇到异常:

.handle(Request.class, (request, headers) -> controller.trigger(request))

Exception: 例外:

nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? 嵌套异常为java.lang.IllegalStateException:未找到线程绑定的请求:您是在实际Web请求之外引用请求属性,还是在原始接收线程之外处理请求? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet 如果您实际上是在Web请求中操作并且仍然收到此消息,则您的代码可能在DispatcherServlet / DispatcherPortlet之外运行

Could someone please tell me what is wrong and how to fix that? 有人可以告诉我哪里出了问题以及如何解决吗? I thought I can just invoke controller method like it was coming from simple POJO. 我以为我可以调用控制器方法,就像它来自简单的POJO。

You are mixing concerns and try to call Web tier from the service layer. 您正在混合考虑,并尝试从服务层调用Web层。

If the logic is like that, then design of the app is wrong. 如果逻辑如此,则该应用程序的设计是错误的。

You should extract some service from the controller logic and call it from the Web, as well as from there on the Integration level. 您应该从控制器逻辑中提取一些服务,并从Web以及集成级别的Web上调用它。

According your stack trace it looks like you try to get access to the request scope object. 根据您的堆栈跟踪,您似乎试图访问request范围对象。 Well, and that is exactly what happens to @Controller beans, I guess. 好吧,我想这正是@Controller bean发生的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM