我在源代码中看到 Spring DATA Rest 可以为具有此 URL 的存储库公开 Json Schema:/{repository}/schema。
有没有人知道如何配置这个?
有 RepositorySchemaController (org.springframework.data.rest.webmvc) 但我还没有找到如何使用它。
版本:2.0.0.M1
我在源代码中看到 Spring DATA Rest 可以为具有此 URL 的存储库公开 Json Schema:/{repository}/schema。
有没有人知道如何配置这个?
有 RepositorySchemaController (org.springframework.data.rest.webmvc) 但我还没有找到如何使用它。
版本:2.0.0.M1
确保设置正确的标题...
Request - /{repository}/schema
Header - Accept: application/json+schema
此外,如果您还没有查看 2.0 快照,那么还有更多功能和更改即将推出
编辑:2014 年 1 月 27 日
更正:
接受应该是“application/schema+json
”而不是“application/json+schema
”
Request - /{repository}/schema
Header - Accept: application/schema+json
2.4.0
只是Spring Data REST版本的更新:
JSONSchema 现在暴露在profile
链接下,因此您需要按如下方式更改您的请求:
Request: /profile/{repository}
Header: Accept: application/schema+json
您可以在此处找到更新的文档。
这实际上返回了ALPS 样式表示。如果您对其他格式感兴趣,例如JSON Schema,您可能希望添加自己的控制器以使其灵活:
@BasePathAwareController
public class JsonSchemaController {
public static final String PROFILE_ROOT_MAPPING = "/schema";
public static final String RESOURCE_PROFILE_MAPPING = PROFILE_ROOT_MAPPING + "/{repository}";
@RequestMapping(value = RESOURCE_PROFILE_MAPPING, method = GET)
public ResponseEntity<JsonNode> descriptor(RootResourceInformation information) {
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(
SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON);
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(information.getPersistentEntity().getType());
return ResponseEntity.ok(jsonSchema);
}
}
Snow的依赖项:
implementation 'com.github.victools:jsonschema-generator:4.16.0'