@ PathVariable get solution with and slash in path

  • 2021-12-04 10:10:02
  • OfStack

@ PathVariable get path with "/" slash

Problem

Access path

http://192.168. 10.243: 1018/hpt/v2/photo/link/2021/7/headimage/super18909261952. jpg, needed/link/2021/7/headimage/super18909261952. jpg value

Solutions


@RequestMapping(value =  " /v2/photo/{photoName}/** " , produces =  " /;charset=utf-8 " , consumes =  " /;charset=utf-8 " )
public void getImageFromNetByUrl(@PathVariable String photoName, HttpServletRequest request, HttpServletResponse response) throws Exception {
        final String pathq =
                request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
        final String bestMatchingPattern =
                request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString();
        String arguments = new AntPathMatcher().extractPathWithinPattern(bestMatchingPattern,pathq);
        String moduleName;
        if(null!= arguments&&!arguments.isEmpty()){
            moduleName = photoName +'/'+ arguments;
        } else {
            moduleName = photoName;
        }
        System.out.println("moduleName"+moduleName);
        }

moduleName is the required value;

@ PathVariable contains special character exceptions such as.

spring MVC has been supporting REST since 3.0, and mainly through @ PathVariable to handle the mapping of request parameters and paths.

Considering SEO, many people like to treat news names as a part of the path. At this time, Chinese names will encounter problems and cannot be mapped. This is due to coding problems. Just find server.xml under TOMCAT/conf and add URIEncoding= "UTF-8" to set URL coding to solve Chinese problems.

In addition, we often encounter a little "." in the path, and the dot is a special character, such as. html,. do, etc., so Spring MVC defaults to the information after the dot as the file suffix, so we need to modify this default value.

In addition, if only this is set at this time, the request can be passed to the controller, but there will be problems with the data passed in the past, and only the data before the last point will be passed, unless you add "/" at the end, such as/news/Test. Point/This will take "Test. Point" as a whole, otherwise you will only get "Test". At this point we can set @ RequestMapping ("/news/{title:. *}")


Related articles: