How to match a Spring @RequestMapping having a @pathVariable containing “/”?

前端 未结 2 1612
心在旅途
心在旅途 2020-12-01 08:32

I am doing the following request from the client:

/search/hello%2Fthere/

where the search term \"hello/there\" has been URLencoded.

On t

相关标签:
2条回答
  • 2020-12-01 09:11

    There are no good ways to do it (without dealing with HttpServletResponse). You can do something like this:

    @RequestMapping("/search/**")  
    public Map searchWithSearchTerm(HttpServletRequest request) { 
        // Don't repeat a pattern
        String pattern = (String)
            request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);  
    
        String searchTerm = new AntPathMatcher().extractPathWithinPattern(pattern, 
            request.getServletPath());
    
        ...
    }
    
    0 讨论(0)
  • 2020-12-01 09:16
        @Configuration
        public class AdditionalWebConfig extends WebMvcConfigurationSupport {
         .......
         ...........
         ...........
        @Override
        public void configureContentNegotiation(ContentNegotiationConfigurer 
        configurer) {
           configurer.favorPathExtension(false);
        }
        .......
        .......
      }
    

    and add regex like this in @PathVariable("user/{username:.+}")

    0 讨论(0)
提交回复
热议问题