基于springboot开发的服务器控制器接收参数及校验

一、控制器获得前端传来的参数

1、url路径带的参数

形式一:/banner/id (id是参数值 比如:1)

在函数参数中使用@PathVariable Integer id,参考示例代码1

注意:参数名字的定义,要跟@GetMapping("/test1/{id}")中的id一致,如果不一致,要使用@PathVariable(name="id")

2)/banner?id=1&name=liangdm

使用@RequestParam String name,参考示例代码1

示例代码1

假设请求url地址为:http://localhost/api/vi/test1/1?name=deming

@GetMapping("/test1/{id}")

public void test1(@PathVariable Integer id,

                          @RequestParam String name) {

    System.out.println("xxxxx");

}

2、获得RequestBody体传递过来的参数,一般是json对象

{

username:"deming",

password:"123456"

}

1) 方法一 :使用@RequestBody Map person 这种方法需要装箱、解构,比较麻烦,不推荐使用

2)方法二:定义数据传输对象 PersonDTO person,使用@RequestBody PersonDTO person来接受参数

步骤一:创建javabean,如使用@Getter、@Setter,需要使用maven安装lombok插件

@Getter

@Setter

public class PersonDTO {

  private String username;

private String password;

}

步骤二:

@PostMapping("/test2")

public PersonDTO test2(@RequestBody PersonDTO person) {

    PersonDTO dto = PersonDTO.builder().name("liangde").age(18).build();

    return dto;

}

二、对获得参数进行校验

使用参数校验,首先在控制器的类上打上@Validated 开启参数校验

@Validated

public class BannerController {

}

1 使用基础内置的验证注解,如:@Range  @Length @Max @Min等,适用于url请求的路径参数

@GetMapping("/test1/{id}")

public void test1(@PathVariable @Range(min = 1, max = 10, message = "id号超出1-10") Integer id

        , @RequestParam @Length(min = 7) String name) {

    System.out.println("this is test1");

}

2、如果是对json对象进行参数校验,需要在@RequestBody 加上@Validated 注解,如下所示

第一步:在javabean对象前,打上@Validated 注解

public PersonDTO test2(@RequestBody @Validated PersonDTO person) {

    PersonDTO dto = PersonDTO.builder().name("liangde").age(18).build();

    return dto;

}

第二步,因为PersonDTO的成员变量是基础参数,对这些基础参数的校验使用基础验证注解就可以了,如下所示:

public class PersonDTO {

    @Length(min=2,max=10,message="姓名的长度应在2~10之间")

    private String name;

}

另外,如果说PersonDTO中有SchoolDTO这种对象成员变量,对这种成员变量的校验,需要打上@Valid注解,这种情况叫级连验证,如下所示

public class PersonDTO {

    @Length(min=2,max=10,message="姓名的长度应在2~10之间")

    private String name;

    @Valid

    private SchoolDTO schoolDTO;

}

注:schoolDTO下的成员变量参数校验使用普通验证注解

其他基础校验注解

​@Null  //被注释的元素必须为null@NotNull  //被注释的元素不能为null@AssertTrue  //被注释的元素必须为true@AssertFalse  //被注释的元素必须为false@Min(value)  //被注释的元素必须是一个数字,其值必须大于等于指定的最小值@Max(value)  //被注释的元素必须是一个数字,其值必须小于等于指定的最大值@DecimalMin(value)  //被注释的元素必须是一个数字,其值必须大于等于指定的最小值@DecimalMax(value)  //被注释的元素必须是一个数字,其值必须小于等于指定的最大值@Size(max,min)  //被注释的元素的大小必须在指定的范围内。@Digits(integer,fraction)  //被注释的元素必须是一个数字,其值必须在可接受的范围内@Past  //被注释的元素必须是一个过去的日期@Future  //被注释的元素必须是一个将来的日期@Pattern(value) //被注释的元素必须符合指定的正则表达式。@Email //被注释的元素必须是电子邮件地址@Length //被注释的字符串的大小必须在指定的范围内@NotEmpty  //被注释的字符串必须非空@Range  //被注释的元素必须在合适的范围内

2 自定义校验注解做参数校验

第一步:定义一个注解类型PasswordEqual

@Documented

@Retention(RetentionPolicy.RUNTIME)

//ElementType.TYPE 注解应用在类中, ElementType.METHOD  注解应用在方法,,ElementType.filed 注解应用在字段

@Target(ElementType.TYPE)

@Constraint(validatedBy = PasswordValidator.class)  //关联类设定PasswordValidator.class,双向关联

public @interface PasswordEqual {

    int min() default 2;

    int max() default 6;

    String message() default "password are not equal"; //出错时,提示消息

//    规范中必须有以下两个模版方法

    Class [] groups() default {};

    Class[] payload() default {};

}

第二步:创建自定义注解的关联类PasswordValidator ,通过实现接口进行注解关联

//第一个参数时自定义注解PasswordEqual,,第二个参数是注解修饰的类型PersonDTO

public class PasswordValidator implements ConstraintValidator {

    private int min;

    private int max;

    private String message;

    @Override

    public void initialize(PasswordEqual constraintAnnotation) {

        this.min = constraintAnnotation.min();

        this.max = constraintAnnotation.max();

        this.message = constraintAnnotation.message();

    }

    @Override

    public boolean isValid(PersonDTO value, ConstraintValidatorContext context) {

        String password1 = value.getPassword1();

        String password2 = value.getPassword2();

        boolean match = password1.equals(password2);

        System.out.println(this.min);

        System.out.println(this.max);

        System.out.println(this.message);

        return match;

    }

}

3、应用注解

@PasswordEqual(min=1,max=100,message="两次密码输入不一致")  //注解打在类上面

public class PersonDTO {

    private String password1;

    private String password2;

}

4、参数校验过程捕获异常,并反馈给前端

前面是使用注解的方法校验参数的异常,一般在后台信息打印异常信息,如果需要把异常信息返回给前端,需要增强框架内容,增加以下方法捕获异常并返回异常信息

1)捕获url路径参数异常

    @ExceptionHandler(ConstraintViolationException.class)

    @ResponseBody

    @ResponseStatus(code=HttpStatus.BAD_REQUEST)

    public UnifyResponse handleConstraintException(HttpServletRequest req, ConstraintViolationException e){

        String requestURL = req.getRequestURI();

        String method = req.getMethod();

        String message = e.getMessage();

        return new UnifyResponse(10001,message,method + " " + requestURL);

    }

2) 捕获DTO参数错误异常,可以同时处理多个参数异常, 状态码统一为参数错误状态码

    @ExceptionHandler(MethodArgumentNotValidException.class)

    @ResponseStatus(code= HttpStatus.BAD_REQUEST)

    @ResponseBody

    public UnifyResponse handleBeanValidation(HttpServletRequest req,MethodArgumentNotValidException e) {

        String requestURL = req.getRequestURI();

        String method = req.getMethod();

        List errors = e.getBindingResult().getAllErrors();

        String message = this.FormatAllErrorsMessage(errors);

        return new UnifyResponse(10001,message,method + " " +requestURL);

    }

    private String FormatAllErrorsMessage( List errors) {

        StringBuffer errMsg = new StringBuffer();

        errors.forEach(error->

                errMsg.append(error.getDefaultMessage()).append(","));

        return errMsg.toString();

    }

你可能感兴趣的:(基于springboot开发的服务器控制器接收参数及校验)