Skip to content
Advertisement

Spring Boot multiple slashes in path variable

I’m trying to do

    @GetMapping(value = "/vault/{key}", produces = "application/json")
    public ResponseEntity<VaultEntity> getEntity(
            @RequestHeader HttpHeaders headers, @RequestHeader(name = "X-Vault-Token") String token,
            HttpServletRequest request, @PathVariable(name = "key") String key) {
        log.info(String.format("%s provided token: %s for key %s", headers.getHost(), token, request.getRequestURI()));
        return vaultService.findByKey(request.getRequestURI(), key, token);
    }

but I want it to match:

/vault/key
/vault/key/key1
/vault/key/key2/key3

and so on

I tried regular expression matching but came up with nothing. What would be the best way to do this?

Advertisement

Answer

you can try like this if key is a static

@RequestMapping(value={"/vault/key", "/vault/key/key1", "/vault/key/key1"}, method = RequestMethod.GET)
public ResponseEntity<VaultEntity> getEntity(
            @RequestHeader HttpHeaders headers, @RequestHeader(name = "X-Vault-Token") String token,
            HttpServletRequest request, @PathVariable(name = "key") String key) {
        log.info(String.format("%s provided token: %s for key %s", headers.getHost(), token, request.getRequestURI()));
        return vaultService.findByKey(request.getRequestURI(), key, token);
    }

if key is a dynamic

@RequestMapping(value={"/vault/{key}", "/vault/{key}/{key1}", "/vault/{key}/{key1}/{key3}"}, method = RequestMethod.GET)
public ResponseEntity<VaultEntity> getEntity(
@PathVariable("key") String key, @PathVariable("key1") String key1, @PathVariable("key3") String key3, @RequestHeader HttpHeaders headers, @RequestHeader(name = "X-Vault-Token") String token,HttpServletRequest request, @PathVariable(name = "key") String key) {
        log.info(String.format("%s provided token: %s for key %s", headers.getHost(), token, request.getRequestURI()));
        return vaultService.findByKey(request.getRequestURI(), key, token);
    }

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement