How to use HAL Browser with Spring Data Rest | SpringBoot + MySQL

In the previous post, we had learned how to create a Spring Data Rest application, and use Postman to navigate arround restAPIs. Are there any tools good for interacting with Spring Data Rest application? -> Yes, Hal Browser is an useful tool for development by using it to follow the restfulAPIs, modify and create a new resource. So in the tutorial, JavaSampleApproach shows how to use Hal Browser with Spring Data Rest.

Related posts:
How to access MySQL database with Spring Data Rest application
Spring Data Rest – How to create a RestAPIs with JPA One-to-Many relational entities | SpringBoot + MySql + HAL Browser

I. Technology

– Java 8
– Maven 3.6.1
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
– HAL Browser

II. Hal Browser

HAL Browser is a web application which we can use by adding a dependency: spring-data-rest-hal-browser. The dependency will autoconfigure the HAL Browser to be served up. So when we visit the uri http://localhost:8080 , we got a first screen shot:

spring hal browser - root uri screen

We have 2 part on the screen: the right side is a response from Spring Data Rest app, the left side is a parsing response of HAL Browser.
There are having 2 group buttons: GET and NON-GET.
GET is used to navigate to a resource of the collection. How about GET buttons with mark icon? -> It is used to build a GET query via Query URI Template.

spring hal browser - queries url template

NON-GET button is used to do a {POST, PUT, DELETE} request on the collection:

spring hal browser - NON-GET button

III. Practices

1. Create Spring Data Rest application

Follow the tutorial to create Spring Data Rest application.
Need add the dependency spring-data-rest-hal-browser in pom.xml file for HAL Browser be served:

2. Run and check results

Build and run the SpringBoot application with commandlines {mvn clean install, mvn spring-boot:run}.

– Check the inital data by SELECT * FROM testdb.customer:

spring hal browser - check initial database

2.1 First access to Hal Browser

Make a request: http://localhost:8080

spring hal browser - first screen

2.2 Select all resources

On the Links part, navigate to customer’s rel row, then press GET button with mark icon:

spring hal browser - customer get all

We can modify Input (JSON) or ignore it with default value, press Follow URI.

-> Results:

spring hal browser - get all customer results

2.3 Get an individual resource

At Embedded Resources, click to customer[0]:

spring hal browser - customer 0

Then click GET button of self link of customer[0]:

spring hal browser - customer[0] resutls

2.3 Discover all queries

Go to http://localhost:8080/customer/search

spring hal browser - search

At the link findByLastName, press mark GET button, we get a Expand URI Template. Modify input with value: "name": "Smith":

spring hal browser - search Smith

Press Follow URI, we got 2 customers having ‘Smith’ for lastname:

spring hal browser - search results

2.4 Post a resource

Go to http://localhost:8080 again, at customer link, press NON-GET button, we get Create/Update pop-up:

spring hal browser - post mary customer

Input Customer info ‘{First name: Mary, Last name: White}’. Then press Make Request button:

spring hal browser - post result

2.5 Modify a resource

At Explorer, go to http://localhost:8080/customer/6:

spring hal browser - put frst

Press NON-GET button at self link:

spring hal browser - put modify input

-> Modify input as below:

  "firstName": "Mary",
  "lastName": "Martin"

– Change action from POST to PUT. Then press Make Request, result:

spring hal browser - put results

Now lastname is modified from ‘White’ to ‘Martin’.

2.6 Delete a resource

At Explorer, go to http://localhost:8080/customer/3.

spring hal browser - delete first step

Press NON-GET button at self link:

spring hal browser - delete change actions

– Change action from POST to DELETE. Then press Make Request:

spring hal browser - delete change actions

-> Final database’s view:

Final Check database

IV. Sourcecode


By grokonez | July 7, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *