ConstraintViolationException in spring rest

后端 未结 2 2185
走了就别回头了
走了就别回头了 2021-01-18 15:36

I have next spring rest controller for handle my exception:

@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
pub         


        
2条回答
  •  余生分开走
    2021-01-18 15:51

    Try to reproduce next steps:

    • Add to your WebConfiguration file
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        return new MethodValidationPostProcessor();
    }
    
    • mark your Controller with @Validated

    • Mark your paramater in Controller's method with

    @Pattern(regexp = "\\d+") @RequestParam String param

    • Then just call that method with parameter like "Some string without digits".


    If you want to use that Entity. Do something like

    @Valid @RequestBody Device device 
    

    in your method. And you can skip all previous steps.

提交回复
热议问题