spring引导验证

lxkprmvk  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(201)

我正在使用带有hibernate验证程序的spring boot,我有一个带有@validated注解的控制器,我想验证api中的所有参数。
其中一个注解不起作用(我调试了它,只是从来没有被检查过,就好像它根本没有被注解一样)。

public MyResponse getPolicyCounters(@RequestBody @Valid MyRequest request) throws Exception {

myrequest类如下所示:

@Valid
public class MyRequest{
    boolean shouldSumSubRules;
    private @Valid RulesSelector rules;

ruleselector类是一个接口,如下所示:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
        @JsonSubTypes.Type(value = RulesStaticList.class, name = "list"),
        @JsonSubTypes.Type(value = RulesDynamicList.class, name = "criteria")
})
@Valid
public interface RulesSelector {
}

rulestaticlist类如下所示:

@Valid
public class RulesStaticList extends ArrayList<@Valid RuleReference> implements RulesSelector {
}

rulerefrence类如下所示:

@JsonDeserialize(using = RuleReferenceDeserializer.class)
@Valid
public interface RuleReference {
    RuleReferenceKind getKind();
}

final class RuleReferenceDeserializer extends StdDeserializer<RuleReference>  {
    public RuleReferenceDeserializer() {
        super(RuleReference.class);
    }

    @Override
    public RuleReference deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String fieldName = p.nextFieldName();
        switch (fieldName) {
            case "tag":
                return p.readValueAs(RuleReferenceByTag.class);
            case "special":
                return p.readValueAs(RuleReferenceBySpecial.class);
            default:
                throw ctxt.wrongTokenException(p, RuleReference.class, JsonToken.FIELD_NAME, "");
        }
    }
}

我有两个rulereference实现,其中一个是rulereferencebytag,如下所示:

@Valid
public class RuleReferenceByTag implements RuleReference {
    @Size(max = 128)
    private String tag;

    public RuleReferenceKind getKind() { return RuleReferenceKind.Tag; }
}

我尽可能地添加了@valid注解,但仍然不起作用。我错过了什么?
其他注解在不同的类中确实有效,但我无法解决此问题:/

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题