Reputation: 18268
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?
Upvotes: 1
Views: 539
Reputation: 194
Try following
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(@RequestParam("id") @Valid @PathVariable @Min(0) @NotNull final Long id)
{
// ...
}
}
Added RequestParam.
Upvotes: 0
Reputation: 1098
Try add the @Validated
to Your Controller.
like this:
@Controller
@RequestMapping(value = "/foo")
@Validated
public class Controller
{
}
Upvotes: 1
Reputation: 40061
0 is valid for @Min(0). Change it to @Min(1) and send in 0...
Upvotes: 1