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

5

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

有人知道如何配置吗?

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

版本:2.0.0.M1

3个回答

3

请确保设置正确的标头...

这对于IT技术非常重要。
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
我尝试了这个命令: curl -v -H "Accept: application/json+schema" http://localhost:8989/books/schema但是我遇到了这个异常: org.springframework.web.HttpMediaTypeNotAcceptableException: 找不到可接受的表示。 - bwilcox
我建议您尝试使用快照而不是里程碑,因为依赖项已经随着代码的更改而改变。 - Stackee007
1
我刚刚找到了我的问题原因。真是让人痛苦 :-) 正确的头部应该是application/schema+json,而不是json+schema。现在它可以工作了,感谢您的帮助。 - bwilcox
我的错误,你发现得很好!我会更新答案,以防其他人遇到这个问题 :) - Stackee007

1

关于Spring Data REST版本2.4.0的更新:

现在可以在profile链接下看到JSONSchema,因此您需要按照以下方式更改请求:

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

你可以在这里找到更新后的文档。

0

这实际上返回ALPS样式表示。 如果您对其他格式感兴趣,例如JSON模式,您可能需要添加自己的控制器以实现灵活性:

@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'

1
默认情况下,它返回ALPS样式,但如果您提供Accept: application/schema+json头,则会返回JSON模式。 - MVinca

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接