0

I have Spring boot-starter-web application with a bunch of RestControllers. It seems that Spring, by default, has some mechanism to redirect requests without a trailing slash to the same request with a trailing slash. So, the following request:

GET http://server/context-name

results in a 302 response with the following location header:

Location: http://server/context-name/

Is there a way to disable this behaviour? I would be happy with a 404 response, but I do not want a redirect.

EDIT: After some research I found out that it's probably Tomcat that is doing the redirect. In a standalone Tomcat this behaviour can be configured with mapperContextRootRedirectEnabled (see Tomcat docs) but I can't figure out how to do this for an embedded Tomcat.

Gert-Jan
  • 752
  • 1
  • 8
  • 21
  • Use `PathMatchConfigurer$setUseTrailingSlashMatchAnswer()` as shown [here](https://stackoverflow.com/a/45258671/5873923). – Marc Tarin Feb 01 '18 at 13:30
  • That option allows to map "/context/users" to "/context/users/" but doesn't prevent Spring from redirecting "/context" to "/context/". – Gert-Jan Feb 01 '18 at 14:34

1 Answers1

1

I had the same problem, and I solved it by changing

<url-pattern>/</url-pattern>

to

<url-pattern>/*</url-pattern>

It was nothing to do with Spring or Tomcat though there are options for both that looks close.

Hope this can help.

Frank R.
  • 1,732
  • 18
  • 21