gpt4 book ai didi

java - 从 Spring 集成流程中调用 Spring Controller

转载 作者:行者123 更新时间:2023-12-01 19:57:41 24 4
gpt4 key购买 nike

嗨,我有一个小问题。我想手动调用 spring Controller ,但有一个异常(exception)。首先,让我向您展示一些集成流程和 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
}
}

当我运行应用程序并发送事件时,我在线遇到异常:

.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? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet

有人可以告诉我出了什么问题以及如何解决吗?我以为我可以调用 Controller 方法,就像它来自简单的 POJO 一样。

最佳答案

您正在混合关注点并尝试从服务层调用 Web 层。

如果逻辑是这样的,那么应用程序的设计就是错误的。

您应该从 Controller 逻辑中提取一些服务,并从 Web 以及集成级别调用它。

根据您的堆栈跟踪,您似乎尝试访问 request 范围对象。嗯,我猜这正是 @Controller beans 发生的情况。

关于java - 从 Spring 集成流程中调用 Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49018199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com