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
Contents
I. Technology
– Java 8
– Maven 3.6.1
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
– MySQL
– 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:
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.
– NON-GET button is used to do a {POST, PUT, DELETE} request on the collection:
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:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-hal-browser</artifactId> </dependency> |
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
:
2.1 First access to Hal Browser
Make a request: http://localhost:8080
2.2 Select all resources
On the Links part, navigate to customer’s rel row, then press GET button with mark icon:
We can modify Input (JSON) or ignore it with default value, press Follow URI.
-> Results:
2.3 Get an individual resource
At Embedded Resources, click to customer[0]
:
Then click GET button of self link of customer[0]
:
2.3 Discover all queries
Go to http://localhost:8080/customer/search
At the link findByLastName, press mark GET button, we get a Expand URI Template. Modify input with value: "name": "Smith"
:
Press Follow URI, we got 2 customers having ‘Smith’ for lastname:
2.4 Post a resource
Go to http://localhost:8080
again, at customer link, press NON-GET button, we get Create/Update pop-up:
Input Customer info ‘{First name: Mary, Last name: White}’. Then press Make Request button:
2.5 Modify a resource
At Explorer, go to http://localhost:8080/customer/6
:
Press NON-GET button at self link:
-> Modify input as below:
{ "firstName": "Mary", "lastName": "Martin" } |
– Change action from POST to PUT. Then press Make Request, result:
Now lastname
is modified from ‘White’ to ‘Martin’.
2.6 Delete a resource
At Explorer, go to http://localhost:8080/customer/3
.
Press NON-GET button at self link:
– Change action from POST to DELETE. Then press Make Request:
-> Final database’s view: