HAL Library Angular 4 to 7
This module needs Angular version 4.3+ since it uses the new HttpClientModule introduced in 4.3
npm i hal-4-angular
- Import AngularHalModule in your app root module
- ResourceService is the entry-point for interacting with Spring Data Rest resources. Their should be only one application-wide ResourceService. So we add it to the providers of our app root module.
NB: Removed API_URI and PROXY_URI in favor of ExternalConfigurationService ExternalConfigurationService allows you to load configurations in a generic way by extending ExternalConfiguration
In simple case proxy and root uri's are a simple string.
First of all let's model our resource entities.
To illustrate we use a simple model of a team and players
Notice that our entity class extends the Resource class.
By inheriting the Resource class we give HAL specific features to our entity
Attention: The name and type of the members of your resource class must exactly match the name and type of the members of the resource entity exposed by your API
Since a Team consists of multiple players, we model the one-to-many relationship between the Team resource and the Player resources
So far so good, time to make our application interact with the API.
To illustrate we create a TeamManagerComponent that will implement some basic CRUD on our resources.
Our component constructor has an argument of type RestService. Upon extends RestService instance.
We have a function getAll() which will fetch all the teams from our backend. To fetch these teams we use the getAll method of the ResourceService. This method requires 2 parameters:
- The type of the resource
- The relative URI path of the resource
i.e. 'teams' for 'http://localhost:8080/teams'
The method will immediately return an empty array. When the response comes in, the array will be automatically populated with fully initialised Team instances.
The array also has an 'observer' property of type Observable<Team> which you can use to listen for incoming data and to handle errors
Every Team instance has hypermedia capabilities. i.e. To get all players of a team, you can simply do the following:
;myTeam.getRelationArrayPlayer, 'players'.subscribemyTeam.players = playersconsole.logerror,this.loading = false;
- The type of the resource
- The name of the relation. The value must match a member of the _links object in the HAL response of the owning resource
This method call will return an array of fully initialised 'Player' instances. Again every Player instance has hypermedia capabilities to further traverse our API.
See the API section of this documentation for all capabilities and options.
Implement SubTypeBuilder interface
This library uses Angular's HTTPClient module under the hood. Just implement your own authentication HttpInterceptor and wire it in your application using the HTTP_INTERCEPTORS Injectiontoken.
- search() in server-side with spring satify findBy* and countBy*
- count() require implementation server-side custom repository method countAll
- addRelation() // add relation
- updateRelation() // update relation
- deleteRelation() // remove relation