Spring Boot Sping Boot -如何使用javax.validation验证传入的嵌套JSON请求对象字段

nimxete2  于 2023-05-06  发布在  Spring
关注(0)|答案(1)|浏览(208)

我使用的是Sping Boot (2.7.6)和javax.validation and swagger v3。
此表单的请求JSON正文

{
    "itemDescription":"vehicle",
    "itemVersion":0,
    "itemCode":{
        "codeType":"1",
        "codeValue":"1111"
    }
}

上面的JSON主体由2个模型ItemRequestItemCode表示为:

import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ItemRequest {

    @NotNull
    @Schema(description = "Item description"
        , requiredMode = Schema.RequiredMode.REQUIRED)
    private String itemDescription;
    
    @NotNull
    @Schema(description = "Unique Item Code consisting of codeType integer and codeValue string"
        , requiredMode = Schema.RequiredMode.NOT_REQUIRED)  
    private ItemCode itemCode;
    
    @Min(value = 0)
    @Schema(description = "Item version'
        , requiredMode = Schema.RequiredMode.NOT_REQUIRED)
    private int itemVersion;
}

,以及它的嵌套JSON模型ItemCode

import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.NotNull;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class ItemCode {

    @NotNull
    @Schema(description="codeType can be 1, 2, or 3"
        , requiredMode = Schema.RequiredMode.REQUIRED))
    private int codeType;
    
    @NotNull
    @Schema(description="Unique itemCode identifier. Can start with 0.  If codeType=1, 3-digits long, if codeType=2, 4-digits long, if codeTpye=3, 5-digits long.",
        , requiredMode = Schema.RequiredMode.REQUIRED)
    private String codeValue;  // for example 021, 5455, 05324
}

如何验证ItemRequest模型中的ItemCode字段,以便验证ItemCode状态的描述?

68bkxrlz

68bkxrlz1#

我解决问题的方式要感谢@Superchamp的建议
创建自定义注解

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ItemCodeValidator.class)
@Documented
public @interface ValidItemCode {

    String message() default "Invalid ItemCode for ItemCode.codeType for ItemCode.codeValue: ${validatedValue}";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default { };
}

添加自定义验证逻辑:

public class ItemCodeValidator implements ConstraintValidator<ValidItemCode, ItemCode> {

    @Override
    public boolean isValid(ItemCode itemCode, ConstraintValidatorContext context) {

        if (itemCode == null) {
            return false;
        }

        switch (itemCode.getCodeType()) {
            case 1:
               return validateItemCode1(itemCode);
            case 2:
                return validateItemCode2(itemCode);
            case 3:
                return validateItemCode3(itemCode);
            default:
                return false;
        }
    }

    private boolean validateItemCode1(Key key) {
       // do your validation
    }

    private boolean validateItemCode2(Key key) {
        // do your validation
    }

    private boolean validateItemCode3(Key key) {
        // do your validation
    }
}

将我的自定义注解添加到有问题的字段:

...
@NotNull
@Schema(description = "Unique Item Code consisting of codeType integer and codeValue string"
        , requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@ValidItemCode 
private ItemCode itemCode;
...

相关问题