如何在JSON模式中定义枚举?

时间:2018-05-01 20:35:12

标签: java json rest

我在Enum架构中声明JSON,如下所示。我使用架构生成了工件而没有任何问题。

目前,我正在使用com.github.fge.jsonschema.* api处理验证部分。 但在验证期间,如果我在没有JSON元素的情况下提交sourceType请求,则API无法验证。需要帮助来解决验证错误。

注意:架构版本为V4

枚举声明

主要架构

    {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "xxxxxxxxxxxxxxxxxxxxxxx",
    "description": "xxxxxxxxxxxxxxxxxxxxx",
    "type": "object",
    "properties": {
        "requestHeader": {
            "$ref": "resource:/json/RequestHeader.json#/definitions/requestHeader"
        },
        "sourceType": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/sourceType"
        },
        "billingSystem": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/billingSystem"
        },
        "siteId": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/siteId"
        },
        "ocnValue": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/ocnValue"
        },
        "raoValue": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/raoValue"
        },
        "tn": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/tn"
        },
        "actionCode": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/actionCode"
        },
        "serviceClass": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/serviceClass"
        },
        "bnsCode": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/bnsCode"
        },
        "privacyIndicator": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/privacyIndicator"
        },
        "customerName": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/customerName"
        },
        "workOrderNumber": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/workOrderNumber"
        },
        "accountNumber": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/accountNumber"
        },
        "portInFlag": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/portInFlag"
        },
        "sendDate": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/sendDate"
        },
        "transferFromAccountNumber": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/transferFromAccountNumber"
        },
        "transferToAccountNumber": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/transferToAccountNumber"
        },
        "comments": {
            "$ref": "resource:/json/LcBase.json#/definitions/lcBase/properties/comments"
        }
    }
}

子模式

{
    "definitions": {
        "lcBase": {
            "type": "object",
            "properties": {
                "transactionId": {
                    "type": "integer",
                    "minLength": 0,
                    "maxLength": 15
                },
                "sourceType": {
                    "type": "string",
                    "enum": [
                        "abc",
                        "xyz"
                    ]
                },......

Java Validator代码

                JsonNode schemaNode = JsonLoader.fromResource(getResource(reqType)); // Root Node .Class
            JsonNode data = JsonLoader.fromString(jsonString);
            JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
            JsonSchema schema = factory.getJsonSchema(schemaNode);
            report = schema.validate(data);
            if (report != null) {
                Iterator<ProcessingMessage> iter = report.iterator();
                while (iter.hasNext()) {
                    ProcessingMessage pm = iter.next();
                    stringBuilder.append(pm.getMessage()).append(",\n");
                }
                result = report.isSuccess();
            }

0 个答案:

没有答案