0

我在日志中收到这些错误消息:

[12:15 11:48:20.957] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�ConstraintValidator�object,object��'}, viewDiscriminator=com.test.entity.view.View$External, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.957] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�object�'}, viewDiscriminator=com.test.entity.view.View$External, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.958] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�Payload�'}, viewDiscriminator=com.test.entity.view.View$External, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.991] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�ConstraintValidator�object,object��'}, viewDiscriminator=null, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.991] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�object�'}, viewDiscriminator=null, validationGroupDiscriminators=[], isResponse=true}
[12:15 11:48:20.992] [ERROR][request id: ffdedde3-f147-4fc2-9f9f-ad0c76c1a868][springfox.documentation.swagger2.mappers.ReferenceModelSpecificationToPropertyConverter] - Unable to find a model that matches key ModelKey{qualifiedModelName=ModelName{namespace='java.lang', name='Class�Payload�'}, viewDiscriminator=null, validationGroupDiscriminators=[], isResponse=true}

似乎以某种方式涉及自定义验证注释并且与它们相关的字段被视为模型或smth。例子:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = CardDocumentValidator.class)
@Documented
public @interface CardDocument {
    String message();

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

public class CardDocumentValidator implements ConstraintValidator<CardDocument, TestEntity> {...}

我正在使用 Spring Boot 2.5.5 和 springfox-boot-starter 3.0.0。不过我还在使用 OpenApi 2.0,所以我配置了 springfox:documentation:swagger:use-model-v3: false 属性,这帮助我摆脱了类似的错误。

在定义中的 api-docs json 中:

        "ConstraintDescriptor«object»": {
            "type": "object",
            "properties": {
                "annotation": {
                    "type": "object"
                },
                "attributes": {
                    "type": "object"
                },
                "composingConstraints": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/ConstraintDescriptor«object»"
                    }
                },
                "constraintValidatorClasses": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«ConstraintValidator«object,object»»'}"
                    }
                },
                "groups": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«object»'}"
                    }
                },
                "messageTemplate": {
                    "type": "string"
                },
                "payload": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«Payload»'}"
                    }
                },
                "reportAsSingleViolation": {
                    "type": "boolean"
                },
                "validationAppliesTo": {
                    "type": "string",
                    "enum": [
                        "IMPLICIT",
                        "PARAMETERS",
                        "RETURN_VALUE"
                    ]
                },
                "valueUnwrapping": {
                    "type": "string",
                    "enum": [
                        "DEFAULT",
                        "SKIP",
                        "UNWRAP"
                    ]
                }
            },
            "title": "ConstraintDescriptor«object»"
        },
        "ConstraintDescriptor«object»DefaultView": {
            "type": "object",
            "properties": {
                "annotation": {
                    "type": "object"
                },
                "attributes": {
                    "type": "object"
                },
                "composingConstraints": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/ConstraintDescriptor«object»DefaultView"
                    }
                },
                "constraintValidatorClasses": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«ConstraintValidator«object,object»»'}"
                    }
                },
                "groups": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«object»'}"
                    }
                },
                "messageTemplate": {
                    "type": "string"
                },
                "payload": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«Payload»'}"
                    }
                },
                "reportAsSingleViolation": {
                    "type": "boolean"
                },
                "validationAppliesTo": {
                    "type": "string",
                    "enum": [
                        "IMPLICIT",
                        "PARAMETERS",
                        "RETURN_VALUE"
                    ]
                },
                "valueUnwrapping": {
                    "type": "string",
                    "enum": [
                        "DEFAULT",
                        "SKIP",
                        "UNWRAP"
                    ]
                }
            },
            "title": "ConstraintDescriptor«object»DefaultView"
        },
        "ConstraintDescriptor«object»ExternalView": {
            "type": "object",
            "properties": {
                "annotation": {
                    "type": "object"
                },
                "attributes": {
                    "type": "object"
                },
                "composingConstraints": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/ConstraintDescriptor«object»ExternalView"
                    }
                },
                "constraintValidatorClasses": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«ConstraintValidator«object,object»»'}"
                    }
                },
                "groups": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«object»'}"
                    }
                },
                "messageTemplate": {
                    "type": "string"
                },
                "payload": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/definitions/Error-ModelName{namespace='java.lang', name='Class«Payload»'}"
                    }
                },
                "reportAsSingleViolation": {
                    "type": "boolean"
                },
                "validationAppliesTo": {
                    "type": "string",
                    "enum": [
                        "IMPLICIT",
                        "PARAMETERS",
                        "RETURN_VALUE"
                    ]
                },
                "valueUnwrapping": {
                    "type": "string",
                    "enum": [
                        "DEFAULT",
                        "SKIP",
                        "UNWRAP"
                    ]
                }
            },
            "title": "ConstraintDescriptor«object»ExternalView"
        },
        "ConstraintValidator«object,object»": {
            "type": "object",
            "title": "ConstraintValidator«object,object»"
        },
        "ConstraintValidator«object,object»DefaultView": {
            "type": "object",
            "title": "ConstraintValidator«object,object»DefaultView"
        },
        "ConstraintValidator«object,object»ExternalView": {
            "type": "object",
            "title": "ConstraintValidator«object,object»ExternalView"
        },
        "ConstraintViolation": {
            "type": "object",
            "properties": {
                "constraintDescriptor": {
                    "$ref": "#/definitions/ConstraintDescriptor«object»"
                },
                "executableParameters": {
                    "type": "array",
                    "items": {
                        "type": "object"
                    }
                },
                "executableReturnValue": {
                    "type": "object"
                },
                "invalidValue": {
                    "type": "object"
                },
                "leafBean": {
                    "type": "object"
                },
                "message": {
                    "type": "string"
                },
                "messageTemplate": {
                    "type": "string"
                },
                "propertyPath": {
                    "$ref": "#/definitions/Path"
                },
                "rootBean": {
                    "type": "object"
                }
            },
            "title": "ConstraintViolation"
        },
        "ConstraintViolationDefaultView": {
            "type": "object",
            "properties": {
                "constraintDescriptor": {
                    "$ref": "#/definitions/ConstraintDescriptor«object»DefaultView"
                },
                "executableParameters": {
                    "type": "array",
                    "items": {
                        "type": "object"
                    }
                },
                "executableReturnValue": {
                    "type": "object"
                },
                "invalidValue": {
                    "type": "object"
                },
                "leafBean": {
                    "type": "object"
                },
                "message": {
                    "type": "string"
                },
                "messageTemplate": {
                    "type": "string"
                },
                "propertyPath": {
                    "$ref": "#/definitions/PathDefaultView"
                },
                "rootBean": {
                    "type": "object"
                }
            },
            "title": "ConstraintViolationDefaultView"
        },
        "ConstraintViolationExternalView": {
            "type": "object",
            "properties": {
                "constraintDescriptor": {
                    "$ref": "#/definitions/ConstraintDescriptor«object»ExternalView"
                },
                "executableParameters": {
                    "type": "array",
                    "items": {
                        "type": "object"
                    }
                },
                "executableReturnValue": {
                    "type": "object"
                },
                "invalidValue": {
                    "type": "object"
                },
                "leafBean": {
                    "type": "object"
                },
                "message": {
                    "type": "string"
                },
                "messageTemplate": {
                    "type": "string"
                },
                "propertyPath": {
                    "$ref": "#/definitions/PathExternalView"
                },
                "rootBean": {
                    "type": "object"
                }
            },
            "title": "ConstraintViolationExternalView"
        },
4

1 回答 1

0

找到罪魁祸首 :) 在基础实体类之一中设置私有 Set validationErrors。因此 ConstraintViolation 被添加到 Models 和 ConstraintDescriptor 及其字段的类中,其中包括:

private final List<Class<? extends ConstraintValidator<T, ?>>> constraintValidatorClasses;

private final Set<Class<?>> groups;

private final Set<Class<? extends Payload>> payloads;

放置@JsonIgnore 有帮助。另一种方法是添加 @ApiModelProperty(hidden = true) ,这不会影响序列化,只需隐藏该字段不被记录。

于 2021-12-15T11:31:11.857 回答