Search code examples
springspring-bootthymeleafspring-webflux

Could not resolve view with name 'index' in Spring Boot


Spring boot: 2.3.3.RELEASE

Java: 11

I use webflux + RouterFunction + Thymeleaf and encounter the error "Could not resolve view with name 'index'".

index.html is under "resources/templates". I put some source code looks important.

Are we not able to use Thymeleaf if we use "RouterFunction"?

Please feel free to put a comment if you need more detail.

######## handler ###########
@Component
public class ItemHandler {

public RouterFunction<ServerResponse> routes = route()
        .path("/item", builder -> builder.GET("/", this::index))
        .build();

public Mono<ServerResponse> index(ServerRequest request) {
    Map<String, Object> attributes = new HashMap<>();
    attributes.put("items", "Hello");

    return ServerResponse.ok().contentType(MediaType.TEXT_HTML)
            .render("index", attributes);
}
}


######## index.html ###########
<!DOCTYPE html>
<html lang="ja"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<h1>FluxTest</h1>
</body>
</html>

######## entry point ###########
@SpringBootApplication
public class DemoWebfluxApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoWebfluxApplication.class, args);
    }
}

Solution

  • The default property in charge of handling the location of static files is spring.resources.static-locations.

    The default values are /META-INF/resources/, /resources/, /static/, /public/. You can either override the default values or put your index.html in one of these locations.