2016-07-27 311 views
7

我試過了一些來自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 

感謝

+0

當前的實際行爲是什麼? –

+0

嗨@HarshilSharma我已經添加了這個。乾杯 – ptimson

+0

您可以使用以下來獲得您的答案http://stackoverflow.com/questions/2886282/integer-separated-by-comma –

回答

20

您需要添加@Validated到類像這樣:

@RestController 
@Validated 
class Controller { 
    // ... 
} 

UPDATE

您需要正確配置它..這個bean添加到您的上下文:

@Bean 
public MethodValidationPostProcessor methodValidationPostProcessor() { 
     return new MethodValidationPostProcessor(); 
} 

實例來處理異常

@ControllerAdvice 
@Component 
public class GlobalExceptionHandler { 
    @ExceptionHandler 
    @ResponseBody 
    @ResponseStatus(HttpStatus.BAD_REQUEST) 
    public Map handle(MethodArgumentNotValidException exception) { 
     return error(exception.getBindingResult().getFieldErrors() 
       .stream() 
       .map(FieldError::getDefaultMessage) 
       .collect(Collectors.toList())); 
    } 


    @ExceptionHandler 
    @ResponseBody 
    @ResponseStatus(HttpStatus.BAD_REQUEST) 
    public Map handle(ConstraintViolationException exception) { 
     return error(exception.getConstraintViolations() 
       .stream() 
       .map(ConstraintViolation::getMessage) 
       .collect(Collectors.toList())); 
    } 

    private Map error(Object message) { 
     return Collections.singletonMap("error", message); 
    } 
} 
+0

謝謝但仍然沒有:(我添加org.springframework.validation.annotation。驗證; – ptimson

+0

@ptimson檢查更新 – Jaiwo99

+0

這給我一個請求運行時錯誤: 2016-07-27 15:37:01.444錯誤10611 --- [nio-8080-exec-1] oaccC [。[。[ ]。[dispatcherServlet]:Servlet.service()用於servlet [dispatcherServlet]與path []的上下文拋出異常[Request processing failed;嵌套的異常是javax.validation.ConstraintViolationException]與根源 javax.validation.ConstraintViolationException:空 \t在org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:136)〜[彈簧上下文4.2。 7.RELEASE ..... – ptimson

0

你可以試試這個

@Pattern(regexp="^[0-9]+(,[0-9]+)*$") 
private static final String VALIDATION_REGEX; 

從你的方法(注意爲final修飾符) 否則

@Pattern() 
private static final String VALIDATION_REGEX = "^[0-9]+(,[0-9]+)*$"; 

,然後刪除@Pattern(正則表達式= VALIDATION_REGEX),只保留@Valid註釋:

public myResonseObject getMyParams(@PathVariable("id") String id, @Valid @RequestParam(value = "myparam", required = true) String myParam) { 
0

您有正確的正則表達式

"^[0-9]+(,[0-9]+)*$" 

它永遠不會解析

1,bob 

也許,你需要:

"^\w+(,\w+)*$" 

如果你也需要解析一個空行,使用:

"^(\w+(,\w+)*)?$"