Spring Data Rest & HAL (Hypertext Application Language)

Tram Ho

Hypertext Application Language aka HAL is used in Spring project when the project has Spring Data Rest. It is used to easily create links to API resources, in simple words, its use is to make it easy for client-side developers or users to easily access and read the Methods available in Spring Data JPA or the returned responses easily.

Illustrations:

image.png

How to use HAL Explorer in spring project

  • First in pom.xml add 2 dependencies as follows:

Then spring will automatically create a bean named profileController, **if there is an Already define error, it is because the controller already has profileController. So let’s change the controller’s name ** Example:

  • That’s it, to access it, just go to the /profile address where there will be specific hrefs of each, when clicked, it will display a meta data about the methods that can be used in spring. data jpa
  • image.png
  • If you want to hide some unnecessary things like pwd or confim pwd, just add the @JsonIgnore annotation.
  • Some examples when using: For example, if I want to list a table of contacts in the order name and size=3, I can localhost:8080/contact?page=0&size=3&sort=name,asc
  • Next, to use the HAL Explorer page, I must first configure it in Application.properties

I leave the base url as /data-api, then just go there and there will be

-And finally, for Secure, use spring-security

Thank you for reading this post

Share the news now

Source : Viblo