2

How can I get my last path variable to be the remaining path? I have something like this, but it's not getting hit:

@RequestMapping(value = "{storageId}/{repositoryId}/{path}/**",
                method = RequestMethod.PUT)
@RequestMapping(value = "{storageId}/{repositoryId}/{path}/**", method = RequestMethod.PUT)
public ResponseEntity upload(@PathVariable(name = "storageId") String storageId,
                             @PathVariable(name = "repositoryId") String repositoryId,
                             @PathVariable(name = "path") String path,
                             MultipartFile multipartFile)
        throws ...
{
   ...
}

In Jersey, I could do it easily like this:

@Path("{storageId}/{repositoryId}/{path:.*}")

... but I have to migrate some code over to Spring MVC.

The problem is that if my URL is:

http://localhost:48080/storages/storage0/snapshots/org/foo/bar/metadata/metadata-foo/3.1-SNAPSHOT/metadata-foo-3.1-20161017.182007-1.jar

my path gets truncated to:

metadata/metadata-foo/3.1-SNAPSHOT/metadata-foo-3.1-20161017.182007-1.jar

Which is obviously incorrect, as it should be:

org/foo/bar/metadata/metadata-foo/3.1-SNAPSHOT/metadata-foo-3.1-20161017.182007-1.jar

Any advice would be welcome!

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125938/discussion-between-carlspring-and-sotirios-delimanolis). – carlspring Oct 17 '16 at 16:56
  • 3
    Take some hints form here: http://stackoverflow.com/questions/3686808/spring-3-requestmapping-get-path-value – Sotirios Delimanolis Oct 17 '16 at 17:17
  • @SotiriosDelimanolis: Any thoughts on http://stackoverflow.com/questions/40095340/is-it-possible-to-use-spring-mvc-with-jersey-annotations/ ? – carlspring Oct 17 '16 at 21:26

2 Answers2

1

It's easy with the new(ish) matching algorithms that started to show up in Spring 5. You just use {*foo} as the path pattern.

@RequestMapping(value = "{storageId}/{repositoryId}/{path}/{*rest}",
                method = RequestMethod.PUT)
public ResponseEntity upload(@PathVariable(name = "storageId") String storageId,
                             @PathVariable(name = "repositoryId") String repositoryId,
                             @PathVariable(name = "path") String path,
                             @PathVariable(name = "rest") String rest,
                             MultipartFile multipartFile)
        throws ...
{
   ...
}

This works by default in Webflux. With MVC you have to switch on the PathPattern matcher with spring.mvc.pathmatch.matching-strategy=path_pattern_parser. I understand this will be the default in Spring Boot 2.6.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
0
 @RequestMapping(value = "{storageId}/{repositoryId}/{path}/**",
                 method = RequestMethod.PUT)
 public ResponseEntity upload(@PathVariable(name = "storageId") String storageId,
                              @PathVariable(name = "repositoryId") String repositoryId,MultipartFile multipartFile,HttpServletRequest request)
            throws ...
    {
        String restOfTheUrl = (String) request.getAttribute(
        HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    }
carlspring
  • 31,231
  • 29
  • 115
  • 197
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • Could try to print all three variables and add the examples? I'm afraid this doesn't work for me. Also, is this really the way it's done in Spring MVC? – carlspring Oct 17 '16 at 20:52
  • Yes, this is really that way: https://stackoverflow.com/questions/3686808/spring-3-requestmapping-get-path-value?noredirect=1&lq=1 – MariuszS Aug 17 '17 at 07:35