关于java:SpringBoot之SpringBoot的HATEOAS基础

11次阅读

共计 3116 个字符,预计需要花费 8 分钟才能阅读完成。

简介

SpringBoot 提供了 HATEOAS 的便捷应用形式,后面一篇文章咱们也讲了如何在 SpringBoot 中应用 HATEOAS。本文将会对这些内容进行扩大深刻,具体解说 SpringBoot 提供的这些根本办法。

链接 Links

HATEOAS 的一个十分重要的特色就是在 resources 资源中蕴含超媒体,而超媒体最简略的示意就是链接。

Spring HATEOAS 为咱们简化了封装 Links 的性能。

咱们看一个 HTML 中的 link 标签的例子:

<head>
<link rel="stylesheet" type="text/css" href="theme.css" />
</head>

能够看到一个 link 有两个比拟重要的属性,一个是 href 代表 link 的链接,还有一个属性是 rel 示意的以后文档与被链接文档之间的关系。

咱们看下 Link 中的要害办法:

    public static Link of(String href) {return new Link(href);
    }

    public static Link of(String href, String relation) {return new Link(href, relation);
    }

    public static Link of(String href, LinkRelation relation) {return new Link(href, relation);
    }

能够传入 href 和 relation 来构建一个 Link 对象。

看上面的例子:

Link link = Link.of("/something");

link = Link.of("/something", "my-rel");

其中 LinkRelation 是关联关系的一个封装接口,留神,它是一个接口,咱们能够应用 IanaLinkRelations 中的具体实现来对其赋值,如下所示:

LinkRelation REL_SELF = IanaLinkRelations.SELF;
LinkRelation REL_FIRST = IanaLinkRelations.FIRST;
LinkRelation REL_PREVIOUS = IanaLinkRelations.PREV;
LinkRelation REL_NEXT = IanaLinkRelations.NEXT;
LinkRelation REL_LAST = IanaLinkRelations.LAST;

URI templates

下面的例子中 link 是指定好的,是动态的。有时候咱们心愿 link 能够依据参数进行变换,那么这样的 link 就是动静的 link,咱们能够通过定义 URI 模板来实现。

所以 Link 还能够通过 UriTemplate 来构建:

    public static Link of(UriTemplate template, String relation) {return new Link(template, relation);
    }

    public static Link of(UriTemplate template, LinkRelation relation) {return new Link(template, relation);
    }

UriTemplate 是对 URI 模板的封装,咱们看一个应用的例子:

Link link = Link.of("/{segment}/something{?parameter}");

Map<String, Object> values = new HashMap<>();
values.put("segment", "path");
values.put("parameter", 42);

assertThat(link.expand(values).getHref()) 
    .isEqualTo("/path/something?parameter=42");

下面的例子中,通过 string 来构建一个 link,而后调用 expand 传入参数对应的 map,来构建实在的 href 值。

除了间接应用 string 之外,还能够传入 UriTemplate:

UriTemplate template = UriTemplate.of("/{segment}/something")
  .with(new TemplateVariable("parameter", VariableType.REQUEST_PARAM);

assertThat(template.toString()).isEqualTo("/{segment}/something{?parameter}");

Link relations

Link relations 指的是 link 中的 ref 属性。代表的是以后文档与被链接文档之间的关系。Spring HATEOAS 中有一个 LinkRelation 类来示意。

IANA(Internet Assigned Numbers Authority)预约义了一些 relations,能够通过 IanaLinkRelations 这个类来获取,如下所示:

Link link = Link.of("/some-resource"), IanaLinkRelations.NEXT);

assertThat(link.getRel()).isEqualTo(LinkRelation.of("next"));
assertThat(IanaLinkRelation.isIanaRel(link.getRel())).isTrue();

Representation models

咱们须要拜访的是一个个的资源,而后须要在一个个的资源中退出 link,Spring HATEOAS 为咱们提供了一个简略的类叫做 RepresentationModel。它蕴含了 Links 和一些很不便的办法来帮忙咱们创立带链接的资源。

最简略的应用办法就是创立一个 RepresentationModel 的子类:

public class BookModel extends RepresentationModel<BookModel> {private final Book content;}

咱们通过 add 办法来对其增加 link:

bookModel.add(linkTo(methodOn(BookController.class).getBook(id)).withSelfRel());

留神,在这种状况下,咱们的 Accept 类型应该是 application/hal+json。

对于简略类型,咱们能够间接应用 EntityModel 对其进行封装:

Person person = new Person("Dave", "Matthews");
EntityModel<Person> model = EntityModel.of(person);

对于汇合,能够应用 CollectionModel:

Collection<Person> people = Collections.singleton(new Person("Dave", "Matthews"));
CollectionModel<Person> model = CollectionModel.of(people);

总结

上解说的 Link,URI templates,Link relations 和 RepresentationModel 就是 Spring HATEOAS 的根底,把握了他们基本上就把握了 Spring HATEOAS。

更多内容请参考 http://www.flydean.com/00043-springboot-hateoas-fundamentals/

最艰深的解读,最粗浅的干货,最简洁的教程,泛滥你不晓得的小技巧等你来发现!

欢送关注我的公众号:「程序那些事」, 懂技术,更懂你!

正文完
 0