Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "Cannot build an application for a request which servlet context does not match with the application that it is being built for." #990

Open
berndgoetz opened this issue Jan 20, 2024 · 4 comments

Comments

@berndgoetz
Copy link

Hello Thymeleaf Team,

I'm running Spring Boot 3.2.1, and the spring-boot-starter-thymeleaf. This pulls Thymeleaf version 3.1.2.RELEASE.

When running the application I get the following error at first access of the thymeleaf template for the home path ("/" or ""):

1/20/2024, 12:43:26 PM [APP/PROC/WEB/0] OUT java.lang.IllegalArgumentException: Cannot build an application for a request which servlet context does not match with the application that it is being built for.
1/20/2024, 12:43:26 PM [APP/PROC/WEB/0] OUT at org.thymeleaf.util.Validate.isTrue(Validate.java:79)
1/20/2024, 12:43:26 PM [APP/PROC/WEB/0] OUT at org.thymeleaf.web.servlet.JakartaServletWebApplication.buildExchange(JakartaServletWebApplication.java:64)
1/20/2024, 12:43:26 PM [APP/PROC/WEB/0] OUT at org.thymeleaf.spring6.view.ThymeleafView.renderFragment(ThymeleafView.java:204)
1/20/2024, 12:43:26 PM [APP/PROC/WEB/0] OUT at org.thymeleaf.spring6.view.ThymeleafView.render(ThymeleafView.java:192) 
...

I found #926 and #937 and they assume it being fixed with 3.1.x. Are we looking at a regression issue here? Thanks.

@berndgoetz
Copy link
Author

The root context problem can be fixed with the following change in JakartaServletWebApplication:

    private boolean servletContextMatches(final HttpServletRequest httpServletRequest) {
        // We should not be directly matching servletContext objects because a wrapper might have been applied
        final String servletContextPath = this.servletContext.getContextPath();
        final String requestServletContextPath = httpServletRequest.getServletContext().getContextPath();
        if (StringUtils.isEmpty(servletContextPath)
                && requestServletContextPath != null
                && requestServletContextPath.equals("/")) {
            return true;
        }
        if (StringUtils.isEmpty(requestServletContextPath)
                && servletContextPath != null
                && servletContextPath.equals("/")) {
            return true;
        }
        return Objects.equals(servletContextPath, requestServletContextPath);
    }

It's the classical "context root identity" issue, i.e. is the context root empty or a slash, such as https://example.com/ vs. https://example.com. I don't know why this validation is necessary in the first place, but just ignoring the differentiation between "" and "/" as the context root makes sense to me. I don't think I can configure that accordingly.

I'm deploying the application into Open Liberty version 23.0.0.8.

@berndgoetz
Copy link
Author

This can be reproduced now in the fork at https://github.com/berndgoetz/spring-petclinic. See the readme there

@berndgoetz
Copy link
Author

Any news on this?

@pablocarle
Copy link

Hi, following the issue as we see the same situation with openliberty 22.0.0.8, deployed in root context path this.servletContext.getContextPath();: "/" and httpServletRequest.getServletContext().getContextPath();: ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants