How to validate Spring MVC @PathVariable values?

前端 未结 6 1829
难免孤独
难免孤独 2020-12-09 03:28

For a simple RESTful JSON api implemented in Spring MVC, can I use Bean Validation (JSR-303) to validate the path variables passed into the handler method?

For examp

6条回答
  •  一生所求
    2020-12-09 04:31

    The solution is simple:

    @GetMapping(value = {"/", "/{hash:[a-fA-F0-9]{40}}"})
    public String request(@PathVariable(value = "hash", required = false) String historyHash)
    {
        // Accepted requests: either "/" or "/{40 character long hash}"
    }
    

    And yes, PathVariables are ment to be validated, like any user input.

提交回复
热议问题