目录
- 1、Caching
- 2、Hazelcast
- 3、Quartz Scheduler
- 4、Sending Email
- 5、Validation
- 6、Calling REST Services
- 7、Web Services
- 8、Distributed Transactions With JTA
- 9、What to Read Next
1、Caching
1.1、Supported Cache Providers
2、Hazelcast
3、Quartz Scheduler
4、Sending Email
5、Validation
5.1、验证手机号是否有效示例
5.1.1、定义验证注解类
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { MobileValidator.class })
public @interface IsMobile {
boolean required() default true;
String message() default "手机号码格式错误";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
5.1.2、定义验证核心类
import static org.springframework.util.StringUtils.hasText;
import java.util.regex.Pattern;
public class MobileValidator implements javax.validation.ConstraintValidator<IsMobile, String> {
private static final Pattern mobile_pattern = Pattern.compile("[1]([3-9])[0-9]{9}$");
private boolean required = false;
@Override
public boolean isValid(String value, javax.validation.ConstraintValidatorContext context) {
if (required) {
return isMobile(value);
} else {
if (!hasText(value)) {
return true;
} else {
return isMobile(value);
}
}
}
@Override
public void initialize(IsMobile constraintAnnotation) {
required = constraintAnnotation.required();
}
private boolean isMobile(String mobile) {
if (!hasText(mobile)) {
return false;
}
return mobile_pattern.matcher(mobile).matches();
}
}
5.1.3、验证异常统一处理
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
return new HashMap<String, Object>(2) {
private static final long serialVersionUID = 6073161079995491657L;
{
put("code", 500);
put("msg", e.getBindingResult().getFieldError().getDefaultMessage());
}
};
}
}
5.1.4、控制器引入验证
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.cnblogs.javalouvre.controller.validator.IsMobile;
@RestController
public class IndexController {
@PostMapping(path = "/")
public Map<String, Object> execute(@Validated @RequestBody User user) {
return new HashMap<String, Object>(3) {
private static final long serialVersionUID = 6073161079995491657L;
{
put("code", 1);
put("msg", "OK");
put("data", Collections.singletonMap("num", UUID.randomUUID().toString().replace("-", "")));
}
};
}
}
class User {
@NotBlank(message = "手机号不可为空")
@IsMobile
private String mobile;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
5.1.5、启动服务验证
5.1.5.1、验证正确
$ curl -L -X POST 'http://127.0.0.1:8080' \
> -H 'Content-Type: application/json' \
> --data-raw '{
> "mobile": "13800138000"
> }'
{"msg":"OK","data":{"num":"94694a9e567241999bd4259aca7b7e7a"},"code":1}
5.1.5.2、验证失败
$ curl -L -X POST 'http://127.0.0.1:8080' -H 'Content-Type: application/json' --data-raw '{
"mobile": "03800138000"
}'
{"msg":"手机号码格式错误","code":500}