一个简单的自定义规则校验注释,校验图片名是不是.jpg
或者.png
校验规则的类
package com.wzw.pdfconverword.validator;
import com.wzw.pdfconverword.annotation.Img;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
//Img注解没创建的时候会报错
public class ImgValidator implements ConstraintValidator<Img, String> {
@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
//s使用了注解的对象的参数值,这里判断它是不是.jpg或者.png的图片
if (s.contains(".jpg") || s.contains(".png")) {
return true;
}
return false;
}
}
自定义校验的注解
package com.wzw.pdfconverword.annotation;
import com.wzw.pdfconverword.validator.ImgValidator;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE}) //注解使用范围
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = ImgValidator.class) //指定校验规则的类
public @interface Img {
String message() default "{img.invalid}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
测试
使用就和校验的注解一样,修饰方法参数或者对象属性。
测试
@GetMapping("/get")
public String get(@Img(message = "图片格式不对") String arg){
System.out.println("------------:"+arg);
return arg;
}
使用全局异常处理,直接返回校验结果
- 全局异常处理代码
package com.wzw.pdfconverword.handler;
import com.wzw.pdfconverword.entity.Result;
import jakarta.validation.ValidationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(ValidationException.class)
public Result<?> validationException(ValidationException e) {
return Result.fail(e.getMessage());
}
}
- 返回结果封装对象
package com.wzw.pdfconverword.entity;
import com.wzw.pdfconverword.ResultCodeEnum;
import lombok.Data;
@Data // Lombok 插件注解。
public class Result<T> {
// 状态码。
private Integer code;
// 信息。
private String message;
// 数据。
private T data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Result() {
}
public static <T> Result<T> build(Integer code, String message, T resultData) {
Result<T> result = new Result<>();
if (resultData != null){
result.setData(resultData);
}
result.setCode(code);
result.setMessage(message);
return result;
}
public static Result success(){
Result result = new Result();
result.setCode(ResultCodeEnum.SUCCESS.getResultCode());
result.setMessage(ResultCodeEnum.SUCCESS.getResultMsg());
return result;
}
public static<T> Result<T> success(T resultData){
Result<T> result = build(ResultCodeEnum.SUCCESS.getResultCode(), ResultCodeEnum.SUCCESS.getResultMsg(), resultData);
return result;
}
public static Result fail(){
Result result = new Result();
result.setCode(ResultCodeEnum.FAIL.getResultCode());
result.setMessage(ResultCodeEnum.FAIL.getResultMsg());
return result;
}
//失败的方法
public static<T> Result<T> fail(T resultData) {
return build(ResultCodeEnum.FAIL.getResultCode(), ResultCodeEnum.FAIL.getResultMsg(), resultData);
}
}
测试返回异常信息
返回成功