spring-data-rest生成的JSON模式不包含“对象”类型的属性定义。
我正在尝试使用生成的架构使用统一材料/自动表单自动创建表单。需要正确定义这些对象属性才能正确创建表单。
curl localhost:8080/api/profile/agentContactRecords -H
"Accept:application/schema+json"
我希望以上架构请求的输出为
...
"Agent Business Lines" : {
"title" : "Agent business lines",
"type" : "array",
"items" : {
"type" : "object",
"properties": {
"Business Line": {
"type": "string",
"title": "The Business line Schema"
},
"Agent Name": {
"type": "string",
"title": "The Agent name Schema"
}
}
}
},
"Agent" : {
"title" : "Agent",
"type" : "object",
"properties": {
"Agent Number": {
"type": "string",
"title": "The Agent number Schema"
}
}
},
...
但是我得到以下内容
...
"Agent Business Lines" : {
"title" : "Agent business lines",
"readOnly" : false,
"type" : "array",
"items" : {
"type" : "object"
}
},
"Agent" : {
"title" : "Agent",
"readOnly" : false,
"type" : "object"
},
...
答案 0 :(得分:0)
我在Spring Darta Rest中检查了PersistentEntityToJsonSchemaConverter.java的源代码,发现它不能确定数组属性内(类型对象的)项的属性。
我找到了一种扩展此类并通过以下方式添加此功能的方法 扩展RepositoryRestMvcConfiguration类及其方法 jsonSchemaConverter。 不幸的是,无法扩展JsonSchema类,因为它在构造函数中使用了包范围的对象(AbstractJsonSchemaProperty)。
答案 1 :(得分:0)
我实体中的对象属性使用@ElementCollection(targetClass =“ className.class”)进行了注释。但是,targetClass本身使用@Entity注释进行了注释。当我将目标类更改为普通的Java对象(没有@Entity注释)时,字段开始以$ ref属性显示在json模式中,如下所示。
"agentBusinessLines" : {
"title" : "Agent business lines",
"readOnly" : false,
"type" : "array",
"items" : {
"$ref" : "#/definitions/agentBusinessLineNonEntityCopy"
}
},
"definitions" : {
"agentBusinessLineNonEntityCopy" : {
"type" : "object",
"properties" : {
"businessLineAcronym" : {
"title" : "Business line acronym",
"readOnly" : false,
"type" : "string"
},
"agentName" : {
"title" : "Agent name",
"readOnly" : false,
"type" : "string"
},
"company" : {
"title" : "Company",
"readOnly" : false,
"type" : "string"
},
"agentNumber" : {
"title" : "Agent number",
"readOnly" : false,
"type" : "integer"
}
}
}