从 Spring 集成流程调用 Spring 控制器
Invoke Spring Controller from Spring Integration flow
嗨,我有一个小问题。我想手动调用 spring 控制器,但我有一个例外。首先,让我向您展示一些集成流程和控制器:
@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))
异常:
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
有人可以告诉我哪里出了问题以及如何解决吗?我以为我可以调用控制器方法,就像它来自简单的 POJO 一样。
你是混合关注,尝试从服务层调用Web层。
如果逻辑是那样,那么应用程序的设计是错误的。
您应该从控制器逻辑中提取一些服务并从 Web 以及集成级别调用它。
根据您的堆栈跟踪,您似乎试图访问 request
范围对象。好吧,我想这正是 @Controller
bean 所发生的事情。
嗨,我有一个小问题。我想手动调用 spring 控制器,但我有一个例外。首先,让我向您展示一些集成流程和控制器:
@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))
异常:
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
有人可以告诉我哪里出了问题以及如何解决吗?我以为我可以调用控制器方法,就像它来自简单的 POJO 一样。
你是混合关注,尝试从服务层调用Web层。
如果逻辑是那样,那么应用程序的设计是错误的。
您应该从控制器逻辑中提取一些服务并从 Web 以及集成级别调用它。
根据您的堆栈跟踪,您似乎试图访问 request
范围对象。好吧,我想这正是 @Controller
bean 所发生的事情。