Spring Data Rest:如何使 json 模式属性成为必需?
•浏览 1
Spring Data Rest: How can I make a json schema property required?
通过阅读另一个问题,我了解到 Spring Data Rest 在 /{resourceName}/schema 处公开了一个 JSON 模式。例如:
GET http://localhost:8080/members/schema
Accept : application/schema+json
{
"name":"org.jxc876.model.Member",
"description":"rest.description.member",
"links": [],
"properties": {
"id": {
"type":"long",
"description":"rest.description.member.id",
"required": false
},
"alias": {
"type":"string",
"description":"rest.description.member.alias",
"required": false
},
"name": {
"type":"string",
"description":"rest.description.member.name",
"required": false
}
}
}
GET http://localhost:8080/members/schema
Accept : application/schema+json
{
"name":"org.jxc876.model.Member",
"description":"rest.description.member",
"links": [],
"properties": {
"id": {
"type":"long",
"description":"rest.description.member.id",
"required": false
},
"alias": {
"type":"string",
"description":"rest.description.member.alias",
"required": false
},
"name": {
"type":"string",
"description":"rest.description.member.name",
"required": false
}
}
}
我想指出某些字段是必需的。我最初尝试使用 Bean Validation 注释 (@NotNull) 和 Jackson (@JacksonProperty) 注释,但似乎都没有触发标志。
注意:我使用的是 Spring Data Rest 2.2.1
我查看了源代码,发现:
- RepositorySchemaController - 映射到 /{repository}/schema
- PersistentEntityToJsonSchemaConverter
- 包含一个创建 Json 模式的 convert(Object, TypeDescriptor, TypeDescriptor) 方法
- 使用另一个类 JsonSchema 及其嵌套的内部类 (Property)
- 该属性包含 3 个字段:类型、描述、必填
- 看起来构造函数调用被硬编码为始终使用 false: new Property(type, message, false)
我想我应该扩展 PersistentEntityToJsonSchemaConverter 并覆盖 convert 方法,然后也覆盖控制器并调用我的自定义 JsonSchemaConverter。