wilx
wilx

Reputation: 18268

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?

Upvotes: 1

Views: 539

Answers (4)

Kamal Kumar
Kamal Kumar

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

min
min

Reputation: 1098

Try add the @Validated to Your Controller.

like this:

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

}

Upvotes: 1

Abhishek Anand
Abhishek Anand

Reputation: 1992

@Min is inclusive of parameter provided.

Upvotes: 1

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40061

0 is valid for @Min(0). Change it to @Min(1) and send in 0...

Upvotes: 1

Related Questions