Search code examples
javaspringvalidation

Spring parameter validation annotation does not validate


I have a controller method like this:

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Controller
@RequestMapping(value = "/foo")
public class Controller
{
    @Autowired Service svc;

    @RequestMapping(value="/bar/{id}", method=RequestMethod.GET)
    @ResponseBody
    BasicResponseDto<ResponseDto>
    bar(@Valid @PathVariable @Min(0) @NotNull final Long id)
    {
        // ...
    }
}

But it seems that the validation annotations do not have any effect. I can give it 0 and it still goes through like its OK.

What do I need to do to make it reject invalid values?


Solution

  • Try add the @Validated to Your Controller.

    like this:

    @Controller
    @RequestMapping(value = "/foo")
    @Validated
    public class Controller
    {
    
    }