简体   繁体   中英

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

You are mixing concerns and try to call Web tier from the service layer.

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.

According your stack trace it looks like you try to get access to the request scope object. Well, and that is exactly what happens to @Controller beans, I guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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