代码如下
Controller:
/**
* 登陆接口
*
* @param loginDto 登陆参数,包含手机号和微信唯一ID
* @return
*/
@ApiOperation(value = "登陆", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
@RateLimiter(value = 1, qps = 1)
@RequestMapping(value = "/login")
public CommonResult login(@Validated(QueryGroup.class) @RequestBody LoginDto loginDto) {
String token = userService.login(loginDto.getMobile(),loginDto.getOpenId());
return CommonResult.success(token,"登陆成功");
}
LoginDto:
/**
* 手机号
*/
@ApiModelProperty(value = "手机号", name = "mobile", dataType = "java.lang.String", required = true)
@Pattern(regexp = "^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\\d{8}$", message = "手机号格式错误", groups = {QueryGroup.class,CreateGroup.class})
@Size(min = 1, max = 20, message = "手机号长度为1-20位", groups = {QueryGroup.class,CreateGroup.class})
private String mobile;
@Pattern和@Size不生效
解决方法
加入以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>