Skip to content

Webjars are not working in WebFlux-powered app #13277

@kshepitko

Description

@kshepitko

Hi,

I have a Spring Boot 2 application (Spring Boot v2.0.1.RELEASE, Spring v5.0.5.RELEASE) that uses WebSockets + JQuery + Bootstrap. Basically I had both org.springframework.boot:spring-boot-starter-webflux and org.springframework.boot:spring-boot-starter-websocket in my dependencies.

My complete dependencies were:

compile("org.springframework.boot:spring-boot-starter-webflux") {
    exclude(module: "spring-boot-starter-tomcat")
}
compile("org.springframework.boot:spring-boot-starter-undertow")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.apache.commons:commons-lang3")
compile("io.micrometer:micrometer-registry-graphite")
compile("org.springframework.boot:spring-boot-starter-amqp")

compile("org.webjars:bootstrap:3.0.3")
compile("org.webjars:jquery:2.0.3-1")
compile("org.springframework.boot:spring-boot-starter-websocket") {
    exclude(module: "spring-boot-starter-tomcat")
}

Today I decided to get rid of usual controllers and WebSockets, and turn to WebFlux routers and Reactive WebSockets. As for libraries, generally I just removed org.springframework.boot:spring-boot-starter-websocket from dependencies.

Surprize! It appeared that static resource serving stopped working at once. OK, I added "org.webjars:webjars-locator:0.34" to classpath, as recommended by docs ("WebJars is also supported via WebJarsResourceResolver and automatically registered when "org.webjars:webjars-locator" is present on the classpath. "), which didn't have any effect (no Webjar mappings are registered on app startup), and then updated my router config to be as follows:

@Configuration
@EnableWebFlux
class MyRouteConfiguration implements WebFluxConfigurer {

    ......................

    @Bean
    RouterFunction<ServerResponse> staticResourceRouter() {
        return resources("/**", new ClassPathResource("static/"));
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("/webjars/")
                .resourceChain(true)
                .addResolver(new WebJarsResourceResolver());
    }

}

This has restored the HTMLs loading from static/, but requests to Webjar resources like JQuery JS and Bootstrap CSS end with

28-05-2018 13:23:10.926+00:00 [XNIO-1 I/O-1] ERROR io.undertow.request - UT005071: Undertow request failed HttpServerExchange{ GET /webjars/bootstrap/3.0.3/css/bootstrap.min.css request {Accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8], Accept-Language=[en-US,en;q=0.9], Cache-Control=[max-age=0], Accept-Encoding=[gzip, deflate, br], User-Agent=[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/66.0.3359.139 Chrome/66.0.3359.139 Safari/537.36], If-Modified-Since=[Wed, 20 Sep 2017 07:36:54 GMT], Connection=[keep-alive], Cookie=[JSESSIONID=p8bHubML-JHZgEy2311GTl15Uu694Wv1CB_7PHyl], Upgrade-Insecure-Requests=[1], Host=[localhost:12228]} response HttpServerExchange{ GET /webjars/bootstrap/3.0.3/css/bootstrap.min.css request {Accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8], Accept-Language=[en-US,en;q=0.9], Cache-Control=[max-age=0], Accept-Encoding=[gzip, deflate, br], User-Agent=[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/66.0.3359.139 Chrome/66.0.3359.139 Safari/537.36], If-Modified-Since=[Wed, 20 Sep 2017 07:36:54 GMT], Connection=[keep-alive], Cookie=[JSESSIONID=p8bHubML-JHZgEy2311GTl15Uu694Wv1CB_7PHyl], Upgrade-Insecure-Requests=[1], Host=[localhost:12228]} response {}}}
java.lang.StackOverflowError: null
	at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.actual(Operators.java:1274)
	at reactor.core.publisher.InnerOperator.currentContext(InnerOperator.java:32)
	at reactor.core.publisher.FluxPeek$PeekSubscriber.currentContext(FluxPeek.java:112)
	at reactor.core.publisher.InnerOperator.currentContext(InnerOperator.java:32)
	at reactor.core.publisher.FluxPeek$PeekSubscriber.currentContext(FluxPeek.java:112)
	at reactor.core.publisher.InnerOperator.currentContext(InnerOperator.java:32)
	at reactor.core.publisher.FluxPeek$PeekSubscriber.currentContext(FluxPeek.java:112)
........................

That's for existing Webjar resources, I checked this: requests for missing ones end properly with 404.

Crazy! And what fixes this is adding back compile("org.springframework.boot:spring-boot-starter-websocket"). But this breaks the routers, which do not work then :(

Am I missing something, or doing something wrong? I would be very thankful for an advice.
Looks like I'm missing a vital dependency, or it's a bug.

Metadata

Metadata

Assignees

Labels

for: stackoverflowA question that's better suited to stackoverflow.com

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions