[spring-mvc]No slash(/) in path variable

Today when I was  upgrading a spring data solr showcase project to spring boot 2.0.0(M2 currently) which can be found HERE, I found that the product detail page can not be shown,giving out the 404 error.

The problem

@RequestMapping("/product/{id}")
public String search(Model model, @PathVariable("id") String id, HttpServletRequest request) {
  model.addAttribute("product", productService.findById(id));
  return "product";
}

The reason was that,the id field of some products contains slash,for example:’EN7800GTX/2DHTV/256M’.In this case,spring parses the slash(/) as a path separator,but not a part of the value of the path variable(see the source of AntPathMatcher).So the request path “/product/EN7800GTX/2DHTV/256MDOES NOT match the pattern “/product/{id}” ,thus a 404 error.

The solution

  @RequestMapping("/product/**")
  public String search(Model model, HttpServletRequest request) {
    String id = extractId(request);
    model.addAttribute("product", productService.findById(id));
    return "product";
  }

  private String extractId(final HttpServletRequest request) {
    // path='/product/EN7800GTX/2DHTV/256M'
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);

    // bestMatchPattern='/product/**'
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    // returns:EN7800GTX/2DHTV/256M
    return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
  }