Unable to resolve view exception with Spring Boot WebFlux when deploying as WAR

Viewed 3098

Facing below exception while deploying Spring WebFlux application as war file in tomcat.Extended with AbstractAnnotationConfigDispatcherHandlerInitializer and implemented getConfigClasses method as below.

Application

@SpringBootApplication
public class TestSpringApplication extends AbstractAnnotationConfigDispatcherHandlerInitializer {

    public static void main(String[] args) {
        SpringApplication.run(TestSpringApplication.class, args);
    }
    @Override
    protected Class<?>[] getConfigClasses() {
        return new Class[]{
            WebConfig.class
        };
    }

Controller

@Controller
public class TestSpringController {

    @RequestMapping("/test")
    public Mono<String> hello(Model model){

         return Mono.just("account");
    }
}

Note : account.html is placed under resources/templates

Exception

java.lang.IllegalStateException: Could not resolve view with name 'account'.
    at org.springframework.web.reactive.result.view.ViewResolutionResultHandler.lambda$resolveViews$3(ViewResolutionResultHandler.java:276) ~[spring-webflux-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) [reactor-core-3.1.1.RELEASE.jar:3.1.1.RELEASE]
2 Answers

Use @RestController instead of @Controller annotation of TestSpringController class

Spring Boot does not currently support WebFlux applications deployed as WAR.

You're here mixing the recommended setup by Spring Framework (see the reference documentation and especially the note that points to AbstractServletHttpHandlerAdapterInitializer) - and the WAR setup for Spring Boot MVC applications.

If you'd like to run a Spring Boot WebFlux app, please consider the executable JAR deployment model.

Related