0
votes

I have a Spring boot application that exposes a REST API via Spring MVC.

When I run my application locally using the embedded tomcat I can access resources with a trailing slash on the end - e.g POST /resource/

However, when I deploy the war to a standalone tomcat instance, I get a 404 if I include the trailing slash on the URL, but a success without the trailing slash - e.g POST /resource.

The embedded tomcat works with or without the trailing slash.

My request mapping is

 @RequestMapping(value = "/resource", method = RequestMethod.POST)

I've tried all sorts of configuration options including

@Override
public void configurePathMatch(PathMatchConfigurer matcher) {
    matcher.setUseRegisteredSuffixPatternMatch(true);
    matcher.setUseTrailingSlashMatch(true);
}

The only difference I can see is the embedded tomcat is v8 and the standalone is v7. Both running the exact same sourcecode but behaving differently.

Can anyone advise on how to correct this issue?

Thanks

1
Why not try standalone Tomcat 8 and see how it behaves?Andy Wilkinson
@AndyWilkinson Good idea... thankfully I've figured it out and won't have to setup a new environment just to debug this!FMC

1 Answers

0
votes

I was able to resolve the issue. For anyone else finding this post...

For some reason, Tomcat 7 was trying to map requests with a trailing slash to a welcome file (index.jsp). It also does not recognise the endpoint if the request contains a trailing slash unless you specifically set a request mapping for "/" despite the setUseTrailingSlashMatch match to true.

This behavior was not mirrored in my embedded tomcat (v8).

I resolved the issue by removing the welcome files from the web.xml and updating my request mapping to:

 @RequestMapping(value = {"/resource","/resource/"}, method = RequestMethod.POST)