0

The following (simplified) code is deployed as a service.

@Configuration
public class RouterConfig {

    private static String htmlContent;

    static {
        try {
            InputStream res = new ClassPathResource("static/index.html").getInputStream();
            htmlContent = new String(res.readAllBytes());
        } catch (IOException e) {
            //
            System.out.println("ERROR");
        }
    }

    @Bean
    public RouterFunction<ServerResponse> indexRouter() {
        return route(GET("/"),
            request -> Optional.ofNullable(htmlContent)
                .map(content ->
                    ServerResponse.ok()
                        .contentType(MediaType.TEXT_HTML)
                        .bodyValue(content))
                .orElse(ServerResponse.status(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED)
                    .build()));
    }

    @Bean
    public RouterFunction<ServerResponse> resourceRouter() {
        return RouterFunctions.resources("assets/**", new ClassPathResource("assets/"))
            .and(RouterFunctions.resources("fonts/**", new ClassPathResource("fonts/")))
            .and(RouterFunctions.resources("img/**", new ClassPathResource("img/")))
            .and(RouterFunctions.resources("/**", new ClassPathResource("/")));
    }
}

If I navigate to the root of my website then it can find the index.html file, which is great.

I defined a 'resource router' which is basically a RouterFunction which accesses my assets, fonts, imgs etc. (See code snippet)

However none of the resources are loaded into my website. Instead I get 404 errors :

enter image description here

My files in my jar file look like this :

enter image description here

And the resources are accessed from the index file like this : enter image description here

How do you correctly configure assets when using spring-webflux Router Functions?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.