我試過了一些來自net的例子,無法讓Spring驗證我的查詢字符串參數。它似乎沒有執行REGEX /失敗。Spring Boot REST @RequestParam未驗證
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!
}
}
當前行爲
PASS - /my/1?myparam=1
PASS - /my/1?myparam=1,2,3
PASS - /my/1?myparam=
PASS - /my/1?myparam=1,bob
期望的行爲
PASS - /my/1?myparam=1
PASS - /my/1?myparam=1,2,3
FAIL - /my/1?myparam=
FAIL - /my/1?myparam=1,bob
感謝
當前的實際行爲是什麼? –
嗨@HarshilSharma我已經添加了這個。乾杯 – ptimson
您可以使用以下來獲得您的答案http://stackoverflow.com/questions/2886282/integer-separated-by-comma –