I am migrating a Spring MVC app from JBoss 7.1.1 to Wildfly 8.1, which has required (encouraged?) me to use the new "undertow" module instead of the older "web" module. Things are coming right along, except that now requests to "/
", which used to invoke the controller method annotated with @RequestMapping("/")
, no longer reach the controller method. Instead, it appears that such requests are being immediately rewritten (not redirected) to "/index.html
". Since I don't have (and have never needed) such a file, all requests for "/
" are now generating 404 errors.
Interestingly, all of the other @RequestMapping
-annotated controller methods continue to function normally.
Here is the relevant snippet from my standalone.xml
file.
<subsystem xmlns="urn:jboss:domain:undertow:1.1">
<buffer-cache name="default"/>
<server name="default-server">
<http-listener name="default" socket-binding="http" max-post-size="4194304"/>
<host name="default-host" alias="localhost">
</host>
</server>
<servlet-container name="default">
<jsp-config development="true"/>
</servlet-container>
</subsystem>
I suspect that when the subsystem
definition for Wildfly's undertow module does not explicitly declare a handler
, Wildfly defaults to a file
handler, which may be responsible for the the URL rewrites -- but I am not certain of this.
Documentation about handlers in the Undertow project, upon which Wildfly's undertow module is based, indicate support for a "Redirect" handler. I have considered using this to work around the unexpected "/" rewriting, but it is not clear to me whether Wildfly's undertow module supports this, and if it does, how to configure it in standalone.xml
. Even if I was able, however, I think it would feel like a hack, and I'd prefer to get to the root of the problem (no pun intended) instead.
There are many S.O. questions describing disappointing RequestMapping("/")
behavior, and many answers suggesting using other paths (such as ""
, "/index"
, etc.), but don't forget: the existing (unchanged) code works just fine in JBoss 7.1.1. (Also, none of those questions mention Wildfly, which is probably the key consideration for this question.) Nevertheless, I experimented with the various suggestions and got nowhere. It simply seems like the URL is being rewritten before it ever reaches the dispatcher servlet.
So, in summary, my question is:
How can I get a Spring MVC app with
RequestMapping("/")
to run in Wildfly 8.1 as it does in JBoss 7.1.1?
In Wildfly, if your web.xml
doesn't have a <welcome-file-list>
element, then one is provided for you, as if you had configured it this way:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
With this default configuration, when Wildfly receives a request for for "/
", the path is automatically rewritten as index.html
. This path will then not match the controller method annotated with RequestMapping("/")
.
JBoss 7 apparently behaves differently, perhaps only referring to the welcome file list after failing to find a matching servlet.
Whatever the cause, you can work around the new behavior by explicitly defining your own welcome file list and including, as the last <welcome-file>
element, an empty welcome file:
<welcome-file></welcome-file>
This allows Wildfly to rewrite "/
" as "/
", in turn allowing the request for "/
" to be processed by the servlet dispatcher (provided that its url-pattern
is set to /
). The servlet dispatcher will then invoke the controller method annotated with RequestMapping("/")
.