Spring Data Rest:如何从存储库公开json模式(2.0.0.M1)

时间:2014-01-16 18:28:12

标签: spring-data-rest

我在源代码中看到Spring DATA Rest可以使用以下URL公开存储库的Json Schema:/ {repository} / schema。

有没有人知道如何配置?

有RepositorySchemaController(org.springframework.data.rest.webmvc),但我还没有找到如何使用它。

版本:2.0.0.M1

3 个答案:

答案 0 :(得分:3)

确保设置正确的标题......

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

答案 1 :(得分:1)

只是Spring Data REST版本2.4.0的更新:

JSONSchema现在公开在profile链接下,因此您需要按如下方式更改您的请求:

Request:  /profile/{repository}
Header:   Accept: application/schema+json

您可以找到更新的文档here

答案 2 :(得分:0)

这实际上返回ALPS style表示形式。如果您对其他格式感兴趣,例如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'