This example demonstrates how to use the spring-hateoas to build json in hal format.
For more information see:
- Open this project in Intellij.
- Create new local tomcat Run/Debug confihuration.
- Add new Build Artifacts in the "Before launch: Make, Build Artifacts" and select "jersey-declarative-linking:war exploded".
- Select available tomcat version.
- Open deployment tab and also add "jersey-declarative-linking:war exploded".
- Save, close and now you can run this project.
http://localhost:8080/rest/albums/1 Accept:application/hal+json
{
"id": "1",
"title": "Heritage",
"artistId": "1",
"stockLevel": 2,
"_links": {
"self": {
"href": "http://localhost:8080/rest/albums/1"
},
"artist": {
"href": "http://localhost:8080/rest/artist/1"
}
}
}
http://localhost:8080/rest/albums/1?embedded=true Accept:application/hal+json
{
"id": "1",
"title": "Heritage",
"artistId": "1",
"stockLevel": 2,
"_links": {
"self": {
"href": "http://localhost:8080/rest/albums/1"
},
"artist": {
"href": "http://localhost:8080/rest/artist/1"
}
},
"_embedded": {
"artist": {
"id": "1",
"name": "Opeth",
"_links": {
"self": {
"href": "http://localhost:8080/rest/albums/1"
}
}
}
}
}
http://localhost:8080/rest/albums Accept:application/hal+json
[
{
"id": "3",
"title": "Monotheist",
"artistId": "2",
"stockLevel": 1,
"_links": {
"self": {
"href": "http://localhost:8080/rest/albums/3"
},
"artist": {
"href": "http://localhost:8080/rest/artist/2"
}
}
},
{
"id": "2",
"title": "Deliverance",
"artistId": "1",
"stockLevel": 3,
"_links": {
"self": {
"href": "http://localhost:8080/rest/albums/2"
},
"artist": {
"href": "http://localhost:8080/rest/artist/1"
}
}
}
]
http://localhost:8080/rest/albums/1?embedded=true application/hal+xml
<resource>
<id>1</id>
<title>album</title>
<stockLevel>2</stockLevel>
<_links>
<_links>
<self>
<href>http://localhost:8080/rest/albums/1</href>
</self>
<musicians>
<href>http://localhost:8080/rest/musics</href>
</musicians>
<artist>
<href>http://localhost:8080/rest/artist/1</href>
</artist>
</_links>
</_links>
<_embedded>
<musicians>
<id>3</id>
<name>Peter</name>
<instrument>saxophone</instrument>
</musicians>
<musicians>
<id>2</id>
<name>David</name>
<instrument>violin</instrument>
</musicians>
<musicians>
<id>1</id>
<name>Kate</name>
<instrument>piano</instrument>
</musicians>
<artist>
<id>1</id>
<name>artist</name>
<_links>
<_links>
<self>
<href>http://localhost:8080/rest/artist/1</href>
</self>
</_links>
</_links>
</artist>
</_embedded>
</resource>