I am trying to create a HAL-compatible REST API using Spring HATEOAS.
After some upheaval, I managed to get to work mostly as expected. (Example) output looks right now:
{
"_links": {
"self": {
"href": "http://localhost:8080/sybil/configuration/bricks"
}
},
"_embedded": {
"brickDomainList": [
{
"hostname": "localhost",
"port": 4223,
"_links": {
"self": {
"href": "http://localhost:8080/sybil/configuration/bricks/localhost"
}
}
},
{
"hostname": "synerforge001",
"port": 4223,
"_links": {
"self": {
"href": "http://localhost:8080/sybil/configuration/bricks/synerforge001"
}
}
}
]
}
}
I don't like the name of the brickDomainList array. It is worth saying "bricks", ideally. How can i change this?
Here is the controller that produces the output:
@RestController
@RequestMapping("/configuration/bricks")
public class ConfigurationBricksController {
private BrickRepository brickRepository;
private GraphDatabaseService graphDatabaseService;
@Autowired
public ConfigurationBricksController(BrickRepository brickRepository, GraphDatabaseService graphDatabaseService) {
this.brickRepository = brickRepository;
this.graphDatabaseService = graphDatabaseService;
}
@ResponseBody
@RequestMapping(method = RequestMethod.GET, produces = "application/hal+json")
public Resources<BrickResource> bricks() {
List<BrickDomain> bricks;
List<BrickResource> resources = new ArrayList<>();
List<Link> links = new ArrayList<>();
Link self = linkTo(ConfigurationBricksController.class).withSelfRel();
links.add(self);
try(Transaction tx = graphDatabaseService.beginTx()) {
bricks = new ArrayList<>(IteratorUtil.asCollection(brickRepository.findAll()));
tx.success();
}
for (BrickDomain brick : bricks) {
self = linkTo(methodOn(ConfigurationBricksController.class).brick(brick.getHostname())).withSelfRel();
BrickResource resource = new BrickResource(brick, self);
resources.add(resource);
}
return new Resources<>(resources, links);
}
}
Are there any annotations or something that I can add to change the name of the array?
If you want / should take a look at the BrickResource class or repositories or something else, look here: https://github.com/ttheuer/sybil/tree/mvctest/src/main/java/org/synyx/sybil
BrickResource is in api / resources /, the repository is in database /, and BrickDomain is in /.
Thank!