Vaadin 14 / Spring Boot: Vaadin takes over all web requests -- how to confi

Hi all,

I’m evaluating vaadin for a project. We’re a spring boot / java / postgreSQL shop. We currently user vuejs for UI, and we hope to
have less context switches when using vaadin for UI.

I have a spring boot (2.3.2.RELEASE) application which defines a REST API and uses springdoc to render a OpenAPI/Swagger UI for discovering
the API.

When I add vaadin (14.3.1) to that project and create views – which does work as intended – we find that vaadin catches all requests, and
thus the swagger UI does no longer work.

How can we configure this?

Setting vaadin.url-mapping to /ui/* gets me only blank pages – no errors – but still no swagger UI.

Am I missing something? I don’t find anything in the docs about this? Is this expected behaviour?

Have you tried vaadin.urlMapping instead of vaadin.url-mapping?

That’s the same – spring boot externalized configuration fuzzy-matches camel case and dasherized config values.

Also, I’m seeing a effect. Not that Swagger UI works, but my Vaadin UI no longer works.

Interestingly – the REST API does work.

Is there anything I could look at? Maybe some configuration customizer for the vaadin servlet?

If you had a non-Spring Boot application, the answer would be indeed that you need a custom VaadinServlet and a @WebServlet annotation which specifies the custom url pattern. I’m not exactly sure how that works with a Spring Boot, though.

Ah!

Is there som docs where can read more about this?

I’m a bit suprised that this doesn’t work OOTB. I briefly looked at the vaadin spring starter auto configuration to no avail. Maybe I need to invest more time.

However, we’re primarily a spring boot shop – this not working raises some red flags for me. I still think I might missing something obvious, though.

For non-Spring Boot applications, it’s mentioned here: https://vaadin.com/docs/v14/flow/advanced/tutorial-application-lifecycle.html#application.lifecycle.servlet-service.servletcustomization

Looks like this Spring configuration tutorial: https://vaadin.com/docs/v14/flow/spring/tutorial-spring-configuration.html#using-spring-boot-properties mentions an additional step required in Spring apps - you need the frontend resource url mapped as well (which makes sense). You can use a ServletRegistrationBean for that, see the code example linked there.

Same issue here. Even tough the configurable URL gets completed the SwaggerUI isn’t accessible.

The response I get next to a 404 which doesn’t occur when Vaadin is disabled:

Could not navigate to 'swagger-ui/index.html'

Reason: Couldn't find route for 'swagger-ui/index.html'

I wasn’t able to find something what prevents me of having this kind of error…

I would also be pretty happy, if we could find a solution for that!

I now suspect that WebJars handling is the culprit. Somehow the vaadin spring boot starter interferes with resource registration wrt web jars.

Will try to debug this next week.

Hi Stefan

There is a [Baeldung post]
(https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) showing how to set up swagger2 properly in java. Have you done all steps described there?

  1. do you have @EnableSwagger2 annotation on a @Configuration class?
  2. Does any of your configuration classes define a Bean for Docket?
  3. do you have the dependency to io.springfox.springfox-swagger-ui ?

I have a Vaadin 14.3.0 application running that has a working swagger UI (on top of its own UI and REST API). I did not have to modify urlmapping or webservlets.

PS: the url to my swagger UI is swagger-ui.html. I’m not sure from the top of my head why we have a difference there.
PSS: I do see in my @Configuration that I have this codeblock, which the baeldung post says it’s only for non-springboot projects (I have spring-boot). Not sure if that matters.

@Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

Kaspar Scherrer:
Hi Stefan

Have you followed all steps described in [this Baeldung post]
(https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)?

  1. do you have @EnableSwagger2 annotation on a @Configuration class? (springfox.documentation.swagger2.annotations.EnableSwagger2)
  2. Does any of your configuration classes define a Bean for Docket?

Please note that the swagger UI works perfectly when I remove Vaadin. I actually started out with plain and simple spring boot, defining a rest service, adding springdocs and making sure that everything works. As soon as I add vaadin and add a UI, the vaadin UI works, but teh SwaggerUI stops working. Interestingly the REST API still works. I’ll do some debugging
later – I still think there’s something amiss with webjar resources.

This will be the most common use case in our shop – having some spring app which need a UI bolted on.

See edits (PS) of my last comment. that could be it. No promises though

Kaspar Scherrer:
See edits (PS) of my last comment. that could be it. No promises though

Hmm. I use the new springdocs OpenAPI V3 instead of swagger: https://springdoc.org

It’s indeed the case that the VaadinServlet’s serverStaticOrWebJarRequest returns NULL for all openapi resources.

I tried to wrap my head around the springdoc’s spring auto configuration – it’s at least called and it registers webjar
resources – but also a transformer. This transformer is there to rewrite requests form “/swagger-ui.html” to the actual
resource location of the swagger UI – they append the openapi UI definition JSON dynamically.

This transformer is never called, and requests to the swagger UI directly also fail. So I believe the vaadin webjar server steals requests.

Interestingly the production build of my code shows the same behaviour. The code comments suggest that the WebJar Server
only active in dev mode, though.

More debugging is needed :confused:

Any update on this guys? I’m struggling with the exact same issue.

Ok. This is really baffling to me. CamelCase versus dasherized is indeed a difference. This is completely counter intuitive.

So, to summarize:

This does work:

vaadin:
	urlMapping: "/ui/*"

This does something – but soes not work:

vaadin:
	url-apping: "/ui/*"

Stefan Eletzhofer:
Ok. This is really baffling to me. CamelCase versus dasherized is indeed a difference. This is completely counter intuitive.

This is a bug from vaadin-spring: https://github.com/vaadin/spring/issues/637

It’s closed but I don’t know if it’s merged the latest vaadin 14.x.

Jean-Christophe Gueriaud:

Stefan Eletzhofer:
Ok. This is really baffling to me. CamelCase versus dasherized is indeed a difference. This is completely counter intuitive.

This is a bug from vaadin-spring: https://github.com/vaadin/spring/issues/637

It’s closed but I don’t know if it’s merged the latest vaadin 14.x.

D’oh. Thanks for the pointers.