Ich habe einige Beispiele aus dem Netz ausprobiert und kann Spring nicht dazu bringen, meinen Abfragezeichenfolgeparameter zu überprüfen. Es scheint nicht, die REGEX/fail auszuführen.
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!
}
}
Aktuelles Verhalten
PASS - /my/1?myparam=1
PASS - /my/1?myparam=1,2,3
PASS - /my/1?myparam=
PASS - /my/1?myparam=1,bob
Gewünschtes Verhalten
PASS - /my/1?myparam=1
PASS - /my/1?myparam=1,2,3
FAIL - /my/1?myparam=
FAIL - /my/1?myparam=1,bob
Vielen Dank
Sie müssen Ihrer Klasse @Validated folgendermaßen hinzufügen:
@RestController
@Validated
class Controller {
// ...
}
UPDATE:
sie müssen es richtig konfigurieren. Fügen Sie diese Bean Ihrem Kontext hinzu:
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
Beispiel für die Behandlung von Ausnahmen :
@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);
}
}
Sie können es versuchen
@Pattern(regexp="^[0-9]+(,[0-9]+)*$")
private static final String VALIDATION_REGEX;
(achten Sie auf den letzten Modifikator) oder auf andere Weise
@Pattern()
private static final String VALIDATION_REGEX = "^[0-9]+(,[0-9]+)*$";
Entfernen Sie dann @Pattern (regexp = VALIDATION_REGEX) aus Ihrer Methode und behalten Sie nur die @Valid -Anmerkung:
public myResonseObject getMyParams(@PathVariable("id") String id, @Valid @RequestParam(value = "myparam", required = true) String myParam) {
Sie haben einen falschen Regex
"^[0-9]+(,[0-9]+)*$"
Es wird niemals analysiert
1,bob
Vielleicht brauchst du:
"^\w+(,\w+)*$"
Und wenn Sie auch eine leere Zeile analysieren müssen, verwenden Sie:
"^(\w+(,\w+)*)?$"