Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-6380

Support for javax.validation annotations on @PathVariable, @RequestHeader, @RequestParam

    Details

    • Type: New Feature
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 3.0 RC2, 4.0.5
    • Fix Version/s: 5.x Backlog
    • Component/s: Web
    • Labels:
    • Last commented by a User:
      true

      Description

      I thought this was already done, because support for controller method-parameter validation should be available for all parameters.

      I there a reason to exclude annotated (@RequestParam, etc.) method parameters? Is it not possible to validate primitive values?

      Simple example:
      For Paging-Support i need two request parameters: "start" & "size". "start" must have a positive value or 0 and "size" must have a positive value and also a maximum set.

        Issue Links

          Activity

          Hide
          dmitry.bedrin@gmail.com Dmitry Bedrin added a comment -

          MethodValidationPostProcessor doesn't work if Controller implements any interfaces. Unless you force proxyTargetClass behavior in Spring AOP of course.

          Show
          dmitry.bedrin@gmail.com Dmitry Bedrin added a comment - MethodValidationPostProcessor doesn't work if Controller implements any interfaces. Unless you force proxyTargetClass behavior in Spring AOP of course.
          Hide
          juergen.hoeller Juergen Hoeller added a comment -

          Along with the InvocableHandlerMethod revision for SPR-15227, I intend to revisit this one for 5.0. We still don't have a Bean Validation API for validating individual values, however, we have the BV 1.1 {ExecutableValidator}} API available by default now and we can probably do some smart invocation for all method arguments, triggered by a method-level @Validated annotation or possibly even just by the mere presence of parameter-level constraint annotations.

          Show
          juergen.hoeller Juergen Hoeller added a comment - Along with the InvocableHandlerMethod revision for SPR-15227 , I intend to revisit this one for 5.0. We still don't have a Bean Validation API for validating individual values, however, we have the BV 1.1 {ExecutableValidator}} API available by default now and we can probably do some smart invocation for all method arguments, triggered by a method-level @Validated annotation or possibly even just by the mere presence of parameter-level constraint annotations.
          Hide
          kakawait Thibaud Lepretre added a comment - - edited

          Using MethodValidationPostProcessor within Spring 4.x following constraint validation works perfectly

          @RestController
          @Validated
          @RequestMapping(path = "/api/v1/users")
          class UserController {
           
              private final UserService userService;
           
              public UserController(UserService userService) {
                  this.userService = userService;
              }
           
              @GetMapping(path = "/")
              public CompletableFuture<Slice<UserResponse>> getUsers(
                      @Min(value = 0) @RequestParam(value = "page", defaultValue = "0") int page,
                      @Min(value = 1) @RequestParam(value = "limit", defaultValue = "50") int limit) {
                  Pageable pageable = new PageRequest(page, limit);
                  return userService.findAll(pageable)
                                    .thenApply(users -> users.map(UserResponse::new));
              }
           
          }
          

          So is there something missing on current version of Spring? (except if Controller implements any interfaces see above)

          Show
          kakawait Thibaud Lepretre added a comment - - edited Using MethodValidationPostProcessor within Spring 4.x following constraint validation works perfectly @RestController @Validated @RequestMapping (path = "/api/v1/users" ) class UserController {   private final UserService userService;   public UserController(UserService userService) { this .userService = userService; }   @GetMapping (path = "/" ) public CompletableFuture<Slice<UserResponse>> getUsers( @Min (value = 0 ) @RequestParam (value = "page" , defaultValue = "0" ) int page, @Min (value = 1 ) @RequestParam (value = "limit" , defaultValue = "50" ) int limit) { Pageable pageable = new PageRequest(page, limit); return userService.findAll(pageable) .thenApply(users -> users.map(UserResponse:: new )); }   } So is there something missing on current version of Spring? (except if Controller implements any interfaces see above)
          Hide
          juergen.hoeller Juergen Hoeller added a comment -

          This works fine for the time being indeed. I'd just like to make it more first-class, without the need for a separate post-processor and without the need for an AOP proxy to begin with. Since we directly dispatch to MVC handler methods, we can also trigger method validation directly, not having to go through a generated proxy for that purpose.

          Show
          juergen.hoeller Juergen Hoeller added a comment - This works fine for the time being indeed. I'd just like to make it more first-class, without the need for a separate post-processor and without the need for an AOP proxy to begin with. Since we directly dispatch to MVC handler methods, we can also trigger method validation directly, not having to go through a generated proxy for that purpose.
          Hide
          kakawait Thibaud Lepretre added a comment -

          Ok is clear now. Thank for information.

          Show
          kakawait Thibaud Lepretre added a comment - Ok is clear now. Thank for information.

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              gbrehmer Gerrit Brehmer
              Last updater:
              Juergen Hoeller
            • Votes:
              61 Vote for this issue
              Watchers:
              67 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                33 weeks, 3 days ago