I have endpoint with request body as String
object, which contains a phone number:
@PostMapping("/phone-number")
public Response phoneNumberRequest(@RequestBody @Valid @Pattern(regexp="myRegexp") String phoneNumber) {
...
}
I have just one request parameter phoneNumber
, so I don't need a JSON object. I need to validate a phone number. But @Valid
annotation doesn't work with @Pattern(regexp = "myRegexp")
when request body is the simple String
object. So my question is why? How can I validate phone number in this case?
I would suggest the following:
@PostMapping("{phone-number}")
public Response phoneNumberRequest(
@PathVariable String phone-number) {
Boolean valid = phone-number.matches("your-regex");
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.