The validation-api package verifies the wording of nested attributes (collection objects)

We know that javax.validation provides the validation-api jar package to implement request parameter verification, avoiding writing some tedious verification logic in the business code.
The following describes a way to write nested attributes.

package com.example.demo.controller;

import com.example.demo.model.ActionParentModel;
import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/hello")
public class DemoController {

@Autowired
private DemoService demoService;

@RequestMapping("/test")
public Boolean test(@RequestBody @Valid ActionParentModel req){
    return true;
}

}

ActionParentModel.class

package com.example.demo.model;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

public class ActionParentModel implements Serializable {
    @Size(min = 1, max = 2)
    private List<List<@Valid ActionModel>> updateConds;

    public List<List<ActionModel>> getUpdateConds() {
        return updateConds;
    }

    public void setUpdateConds(List<List<ActionModel>> updateConds) {
        this.updateConds = updateConds;
    }
}

ActionModel .class

package com.example.demo.model;

import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

public class ActionModel {


    /**
     * 条件类型(1:宝马,2:奔驰)
     */
    @NotNull
    @Range(min = 1, max = 2)
    private Integer keyType;

    @Pattern(regexp = "\\w+", message = "属性名格式不正确")
    private String keyName;

    public Integer getKeyType() {
        return keyType;
    }

    public void setKeyType(Integer keyType) {
        this.keyType = keyType;
    }

    public String getKeyName() {
        return keyName;
    }

    public void setKeyName(String keyName) {
        this.keyName = keyName;
    }
}

List<List<@Valid ActionModel>> updateConds; @Valid here must be placed before ActionModel, otherwise the validation is invalid.

Note that the validation-api here must be 2.0.1.Final, version 1.1.0.Final is not supported.

Compare the source code difference of the two versions
2.0.1.Final:

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Valid {
}

1.1.0Final:

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Valid {
}

2.0.1.Final has more ElementType.TYPE_USE support.

maven dependency

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

<dependency>
  <groupId>org.hibernate.validator</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>6.0.16.Final</version>
</dependency>

Insert picture description here

Guess you like

Origin blog.csdn.net/huangdi1309/article/details/89673875