0

I cant solve this problem (@PathVariable with slashes in middle of @GetMapping, @DeleteMapping URL) help me please!

URL ex : aaa/test/111/l2323:sdfsd:23423423/bbb 

- test/111/l2323:sdfsd:23423423 : string variable with slashes
- bbb : another variable

    @GetMapping("aaa/**")
    public @ResponseBody List<Dto> getAAA(HttpServletRequest request) {
        ...
    }

    @DeleteMapping("aaa/**/{bbb}")
    public void deleteTest(HttpServletRequest request,
        @PathVariable("bbb") String bbb) {
        ...
    }

    @GetMapping("aaa/**/{bbb}")
    public Dto getTest(HttpServletRequest request,
        @PathVariable("bbb") String bbb) {
         ...
    }

    private String extractSlashVariable(HttpServletRequest request) {

        String path = (String) request.getAttribute(
            HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); 
        String bestMatchPattern = (String) request.getAttribute(
            HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); 
        return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
    }..

how can I handle this problems? The above codes are not working... Help me please Thank you!!

Jelly
  • 11
  • 2
  • Uh... your example is not clear and does not match any of your mappings because it doesn't have `aaa` part. Could you provide a better one? With clear distinction of what is the value of the path parameter that you want? – Deltharis Feb 28 '22 at 08:25
  • @Deltharis, sorry for the Insufficient data – Jelly Feb 28 '22 at 08:40
  • @Deltharis, URL example : aaa/{string variable containing slashes}/{bbb} – Jelly Feb 28 '22 at 08:40
  • URL ex : aaa/test/111/l2323:sdfsd:23423423/bbb – Jelly Feb 28 '22 at 08:41
  • test/111/l2323:sdfsd:23423423 : string variable with slashes – Jelly Feb 28 '22 at 08:41
  • bbb : another variable – Jelly Feb 28 '22 at 08:41
  • 1
    Arent you getting ambiguity errors for using @GetMapping("aaa/**/{bbb}") and "aaa/**" ?? – Antoniossss Feb 28 '22 at 08:43
  • @Antoniossss there is error, but it not ambiguity errors – Jelly Feb 28 '22 at 08:52
  • Description: Invalid mapping pattern detected: /aaa/**/{bbb} ^ No more pattern data allowed after {*...} or ** pattern element Action: Fix this pattern in your application or switch to the legacy parser implementation with 'spring.mvc.pathmatch.matching-strategy=ant_path_matcher'. – Jelly Feb 28 '22 at 08:52
  • No wonder no ambiguity - it just rejected the pattern in the first place ;) – Antoniossss Feb 28 '22 at 08:55
  • @Antoniossss how can i rejected the pattern in the first place? – Jelly Feb 28 '22 at 09:19
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Feb 28 '22 at 14:56
  • I solve this problem with @RequestParam! thank you all who reply to me! – Jelly Mar 02 '22 at 07:44

1 Answers1

0

A principled answer would be - you can't. URLs exist with the assumption of a structure - each part being separated by a slash. Having a "variable" containing slashes in your url is an invalid thing to take in as a rule, a variable can't span multiple levels of the url. If a path parameter can contain problematic characters like slashes they need to be url-encoded (%2F I believe is the encoded version of /).

If you really want to just hack it to work - if you do set the ant matching strategy to ant_path_matcher (that allows matching paths like /**/test) like the error message suggests it might allow you to parse it. You will get ambiguity error between your two GETs, that will be impossible, but having just one of them and the delete miiiight work. To do that just add spring.mvc.pathmatch.matching-strategy=ant_path_matcher to your application.properties.

Deltharis
  • 2,320
  • 1
  • 18
  • 29