I have tried a number of examples from the net and cannot get Spring to validate my query string parameter. It doesn't seem execute the REGEX / fail.
package my.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@RestController
public class MyController {
private static final String VALIDATION_REGEX = "^[0-9]+(,[0-9]+)*$";
@RequestMapping(value = "/my/{id}", method = GET)
public myResonseObject getMyParams(@PathVariable("id") String id,
@Valid @Pattern(regexp = VALIDATION_REGEX)
@RequestParam(value = "myparam", required = true) String myParam) {
// Do Stuff!
}
}
Current behaviour
PASS - /my/1?myparam=1
PASS - /my/1?myparam=1,2,3
PASS - /my/1?myparam=
PASS - /my/1?myparam=1,bob
Desired behaviour
PASS - /my/1?myparam=1
PASS - /my/1?myparam=1,2,3
FAIL - /my/1?myparam=
FAIL - /my/1?myparam=1,bob
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…