我使用的是Sping Boot (2.7.6)和javax.validation and swagger v3。
此表单的请求JSON正文
{
"itemDescription":"vehicle",
"itemVersion":0,
"itemCode":{
"codeType":"1",
"codeValue":"1111"
}
}
上面的JSON主体由2个模型ItemRequest
和ItemCode
表示为:
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
状态的描述?
1条答案
按热度按时间68bkxrlz1#
我解决问题的方式要感谢@Superchamp的建议
创建自定义注解
添加自定义验证逻辑:
将我的自定义注解添加到有问题的字段: