RedirectAttributes不能与Spring 5一起使用

时间:2017-09-28 16:51:22

标签: java spring spring-boot spring-webflux

我在Spring Webflux应用程序上,我想在我的一个控制器上执行重定向:

@Controller
@RequestMapping(value = "/login")
public class AuthenticationController {

    @Autowired
    private AuthenticationFacade authenticationFacade;

    @GetMapping
    public String loginView(final Model model) {

        model.addAttribute("authenticationForm", new AuthenticationForm());

        return "login";
    }

    @PostMapping
    public Rendering authenticate(@Valid final AuthenticationForm authenticationForm,
                                  final BindingResult bindingResult, final RedirectAttributes redirectAttributes) {

        if (!bindingResult.hasErrors()) {
            final AccountDto authenticatedAccount =
                    this.authenticationFacade.authenticate(authenticationForm).block();

            if (authenticatedAccount != null) {
                return Rendering.redirectTo("/home").build();
            }
        }

        return Rendering.view("login").build();
    }
}

我知道Rendering.redirectTo()将重定向作为RedirectView执行。但是,当我在我的控制器方法参数中添加RedirectAttributes以提供重定向属性时,我有以下异常:

2017-09-28 18:35:08.084 ERROR 3702 --- [nio-9000-exec-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : Failed to handle request

java.lang.IllegalStateException: Failed to invoke handler method with resolved arguments: [0][type=org.springframework.validation.support.BindingAwareConcurrentModel][value={}],[1][type=org.springframework.validation.support.BindingAwareConcurrentModel][value={}] on public java.lang.String fr.auchan.wheatley.front.controller.AuthenticationController.loginView(org.springframework.ui.Model,org.springframework.web.servlet.mvc.support.RedirectAttributes)
    at org.springframework.web.reactive.result.method.InvocableHandlerMethod.lambda$invoke$0(InvocableHandlerMethod.java:131)
    at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:118)
    at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1010)
    at reactor.core.publisher.MonoWhen$WhenCoordinator.signal(MonoWhen.java:246)
    at reactor.core.publisher.MonoWhen$WhenInner.onNext(MonoWhen.java:306)
    at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:198)
    at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:1567)
    at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.request(FluxPeekFuseable.java:139)
    at reactor.core.publisher.MonoWhen$WhenInner.onSubscribe(MonoWhen.java:296)
    at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onSubscribe(FluxPeekFuseable.java:173)
    at reactor.core.publisher.MonoJust.subscribe(MonoJust.java:54)
    at reactor.core.publisher.MonoPeekFuseable.subscribe(MonoPeekFuseable.java:74)
    at reactor.core.publisher.Mono.subscribe(Mono.java:2769)
    at reactor.core.publisher.MonoWhen$WhenCoordinator.subscribe(MonoWhen.java:177)
    at reactor.core.publisher.MonoWhen.subscribe(MonoWhen.java:127)
    at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)
    at reactor.core.publisher.MonoPeekFuseable.subscribe(MonoPeekFuseable.java:74)
    at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)
    at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
    at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:147)
    at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)
    at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:148)
    at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:67)
    at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:76)
    at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.innerNext(FluxConcatMap.java:270)
    at reactor.core.publisher.FluxConcatMap$ConcatMapInner.onNext(FluxConcatMap.java:790)
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:115)
    at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:1567)
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:156)
    at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.set(Operators.java:1381)
    at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.onSubscribe(Operators.java:1255)
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)
    at reactor.core.publisher.MonoJust.subscribe(MonoJust.java:54)
    at reactor.core.publisher.MonoMapFuseable.subscribe(MonoMapFuseable.java:59)
    at reactor.core.publisher.Mono.subscribe(Mono.java:2769)
    at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:414)
    at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:210)
    at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:91)
    at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:55)
    at reactor.core.publisher.FluxConcatMap.subscribe(FluxConcatMap.java:121)
    at reactor.core.publisher.MonoNext.subscribe(MonoNext.java:40)
    at reactor.core.publisher.MonoSwitchIfEmpty.subscribe(MonoSwitchIfEmpty.java:44)
    at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)
    at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)
    at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)
    at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)
    at reactor.core.publisher.Mono.subscribe(Mono.java:2769)
    at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:165)
    at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)
    at reactor.core.publisher.Mono.subscribe(Mono.java:2769)
    at org.springframework.http.server.reactive.ServletHttpHandlerAdapter.service(ServletHttpHandlerAdapter.java:116)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

这是我的配置: - Spring Boot 2.0.0.M3(Spring 5.0.0.RC3)。 - Spring Webflux

使用Spring 5以这种方式提供重定向属性是不可能的吗?还有另一种方法可以做到这一点,还是一个错误?

3 个答案:

答案 0 :(得分:4)

你正试图在Spring WebFlux中使用org.springframework.web.servlet.mvc.support.RedirectAttributes,一个Spring MVC类。这不会起作用。

我不知道WebFlux中的任何等效内容,这可能是一个增强请求。

答案 1 :(得分:0)

我不完全确定它在Spring 5中是如何工作的。但是,从根本上说,当您重定向到不同的URL时,它会成为对HTTP服务器的新请求。这意味着,服务器需要新重定向请求中的所有必需属性和参数才能访问它。

我认为,这就是你的情况下的问题,你的控制器期望一个参数由于使用重定向生成的这个新请求而丢失。尝试找到转发此请求的方法,而不是重定向。这将包含您的终端控制器请求中的所有原始属性。

我不确定,如果我能正确理解这个问题,可以对这个问题解决方案有所了解。

答案 2 :(得分:0)

正如答案中所提到的,目前Spring Webflux中似乎不存在等效的 RedirectAttributes

我在Spring开了一个JIRA,要求改进,如果有些人有兴趣获得关于这一点的更新,请点击这里的链接:https://jira.spring.io/browse/SPR-16026