springboot:handling_the_trailing_slash_in_url

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.

  1. Write a WebConfiguration class with @Configuration annotation that implements WebMvcConfigurer.
  2. @Override that method public void configurePathMatch(PathMatchConfigurer configurer){…}
  3. 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.

  • springboot/handling_the_trailing_slash_in_url.txt
  • Last modified: 2020/06/01 14:51
  • by chongtin