Introduction

SpringBoot provides a convenient way to use HATEOAS. In the previous article, we also talked about how to use HATEOAS in SpringBoot. This article will expand on these contents and explain in detail the basic methods provided by SpringBoot.

Links

A very important feature of HATEOAS is the inclusion of hypermedia in resources, and the simplest representation of hypermedia is a link.

Spring HATEOAS simplifies the function of encapsulating Links for us.

Let's look at an example of a link tag in HTML:

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

It can be seen that a link has two more important attributes, one is the link that href represents the link, and the other is the relationship between the current document and the linked document represented by rel.

Let's look at the key methods in 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);
    }

You can pass in href and relation to construct a Link object.

See the example below:

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

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

Among them, LinkRelation is an encapsulation interface of the association relationship. Note that it is an interface, and we can use the specific implementation in IanaLinkRelations to assign values to it, as shown below:

 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

In the above example, the link is specified and static. Sometimes we hope that the link can be transformed according to the parameters, then such a link is a dynamic link, which can be achieved by defining a URI template.

So Link can also be constructed through 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 is an encapsulation of URI templates, let's see an example of use:

 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");

In the above example, a link is constructed by string, and then expand is called to pass in the map corresponding to the parameter to construct the real href value.

In addition to using string directly, you can also pass in 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 refer to the ref attribute in the link. Represents the relationship between the current document and the linked document. There is a LinkRelation class in Spring HATEOAS to represent it.

IANA (Internet Assigned Numbers Authority) predefines some relations, which can be obtained through the IanaLinkRelations class, as follows:

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

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

Representation models

What we need to access is each resource, and then we need to add links to each resource. Spring HATEOAS provides us with a simple class called RepresentationModel. It contains Links and some handy methods to help us create linked resources.

The easiest way to use it is to create a subclass of RepresentationModel:

 public class BookModel extends RepresentationModel<BookModel> {

    private final Book content;

}

We add a link to it through the add method:

 bookModel.add(linkTo(methodOn(BookController.class).getBook(id)).withSelfRel());
Note that our Accept type should be application/hal+json in this case.

For simple types, we can directly use EntityModel to encapsulate it:

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

For collections, you can use CollectionModel:

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

Summarize

The Link, URI templates, Link relations and RepresentationModel explained above are the foundation of Spring HATEOAS, and if you master them, you will basically master Spring HATEOAS.

For more information, please refer to http://www.flydean.com/00043-springboot-hateoas-fundamentals/

The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tricks you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com