Handling the Trailing Slash in URL
For now, if you use @RequestMapping, @GetMapping, @PostMapping, … you will see that if you accidentally added a trailing slash “/” in the URL, you will get a 404 HTTP error.
To handling this problem, we need to add a path matching configurer.
- Write a WebConfiguration class with @Configuration annotation that implements WebMvcConfigurer.
- @Override that method
public void configurePathMatch(PathMatchConfigurer configurer){…}
- The method looks like:
@Override public void configurePathMatch(PathMatchConfigurer configurer){ configurer.setUseTrailingSlashMatch(true); }
To manipulate the URL, you can chain this configurer with setPathHelper(…)
, or setUrlPathHelper(…)
with your own implementation of PathMatcher
or UrlPathHelper
.