Joh*_*Lim 8 rest hateoas spring-data-rest spring-hateoas
我正在尝试将HATEOAS与Spring HATEOAS一起使用,并且需要使用Spring HATEOAS将enums作为REST API公开.
我尝试了三种方法如下:
@RestController
@RequestMapping(path = "/fruits")
public class FruitResourceController {
@RequestMapping(method = RequestMethod.GET)
public Fruit[] fruits() {
return Fruit.values();
}
// NOTE: The `produces` attribute is only for browsers.
@RequestMapping(path = "/with-resource", method = RequestMethod.GET,
produces = MediaTypes.HAL_JSON_VALUE)
public Resource<Fruit[]> fruitsWithResource() {
Resource<Fruit[]> resource = new Resource<Fruit[]>(Fruit.values());
Link selfLink = linkTo(methodOn(FruitResourceController.class).fruitsWithResource())
.withSelfRel();
resource.add(selfLink);
return resource;
}
// NOTE: The `produces` attribute is only for browsers.
@RequestMapping(path = "/with-resources", method = RequestMethod.GET,
produces = MediaTypes.HAL_JSON_VALUE)
public Resources<Fruit> fruitsWithResources() {
Resources<Fruit> resources = new Resources<Fruit>(Arrays.asList(Fruit.values()));
Link selfLink = linkTo(methodOn(FruitResourceController.class).fruitsWithResources())
.withSelfRel();
resources.add(selfLink);
return resources;
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道哪个是HATEOAS的正确方法.任何建议或参考将不胜感激.
作为参考,我有以下Spring Data REST配置:
@Configuration
public class SpringDataRestConfig {
@Bean
public ResourceProcessor<RepositoryLinksResource> repositoryLinksResourceProcessor() {
return new ResourceProcessor<RepositoryLinksResource>() {
@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
Link fruitsLink = linkTo(methodOn(FruitResourceController.class).fruitsWithResources())
.withRel("fruits");
resource.add(fruitsLink);
return resource;
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
有关示例项目,请参阅以下内容:
https://github.com/izeye/spring-boot-throwaway-branches/blob/data-jpa-and-rest/src/main/java/com/izeye/throwaway/SpringDataRestConfig.java https://github.com /izeye/spring-boot-throwaway-branches/blob/data-jpa-and-rest/src/main/java/com/izeye/throwaway/FruitResourceController.java
---更新于2016.01.04
使用ALPS(/profile)看起来很容易获得枚举,但我不确定这是一个正确的方法.
HATEOAS 个人资料似乎是正确的地方。但当你手里拿着锤子时,一切看起来都像钉子。
数据验证不是 HATEAOS 规范的一部分,您不应该尝试“用锤子粉刷墙壁”。
如果您使用专门为此设计的解决方案,您的情况会好得多;形式。我应该研究 JSON-Schema 及其扩展 Json-Forms。
这是通过 UI 定义数据输入的标准化解决方案。仅代码端点释义 HATEAOS 命名约定
/表单/${实体名称}
作为 /profile 的扩展/替代,您可以相当容易地修改 Spring 以将其包含到根和实体的 HATEOAS“_links”中。但这不是 HATEOAS!(或斯巴达!)
这将返回同名实体的硬编码 JSON 表单架构。您还可以使用交叉连接半自动执行此操作,以便它仅返回当前实体的匹配键/字段。因此,如果实体发生更改,它不会完全破坏您的 UI。然后,您可以使用大量代码实现完全自动化,这些代码通过反射读取实体类并自动为您的实体生成 JSON 表单架构定义。
就像这个 Java 风格的伪代码
entity.getFields().forEach(
if (isEnumeration(field)) {
sb.append(
field.getName() + ": {"
+ "type: string,"
+ "title: " + localize(field.getName() + ","
+ "enum: " Arrays.toString(field.values()) + ","
+ "required: " isNullable(field)
+ " },"
);
continue;
);
if (isString(field)) {
...
Run Code Online (Sandbox Code Playgroud)
限制你自己的数据模式是可行的。如果您的代码和 ER 干净且一致。您还可以添加更多 UI 验证信息,例如正则表达式验证。React、Angular 和 Vue 都有符合该标准的现成 UI 库和组件。Materials UI 组件基于此,至少它们的 Forms 组件也是如此。
| 归档时间: |
|
| 查看次数: |
971 次 |
| 最近记录: |