简介

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/

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

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