简体   繁体   English

如何从Java过滤器调用Spring Controller

[英]How to invoke a Spring Controller from Java Filter

This is my scenario: I have two applications, both use Spring MVC. 这是我的情况:我有两个应用程序,都使用Spring MVC。 I have a Filter class in one of them and a Controller class in the other one. 我在其中一个中有一个Filter类,在另一个中有一个Controller类。 The requirement is a little weird, when a request is made to the application that contains the filter, this filter should invoke the controller in the another application to "keep alive" the session so I need invoking from filter class a controller class and pass as parameter the JSESSIONID. 要求有点怪异,当对包含过滤器的应用程序发出请求时,此过滤器应调用另一个应用程序中的控制器以“保持活动”会话,因此我需要从过滤器类调用控制器类并通过参数JSESSIONID。 Any suggestions? 有什么建议么?

Thanks! 谢谢!

At the end, I achieve my goal using AsyncRestTemplate , the call to the controller from filter was something like this: 最后,我使用AsyncRestTemplate实现了我的目标,从过滤器对控制器的调用是这样的:

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Cookie", "JSESSIONID=" + sessionId);

        HttpEntity<String> httpEntity = new HttpEntity<String>(null, httpHeaders);

        String controllerUrl = CONTROLLER_URL;
        ListenableFuture<ResponseEntity<String>> responseEntityListenableFuture = restTemplate.exchange(controllerUrl, HttpMethod.GET, httpEntity, String.class);

        responseEntityListenableFuture.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
            @Override
            public void onSuccess(ResponseEntity<String> stringResponseEntity) {
                // Nothing to do
            }

            @Override
            public void onFailure(Throwable throwable) {
                // Handle your error
            }
        });

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

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