I've read the following reference (Spring HATEOAS & HAL: Change array name in _embedded) and I tried to change the array name in the _embedded tag.
I don't want to put any reference to the (Spring) framework in my domain object (using @Relation).
Nor I don't want to use a specific library (evo-inflector) in my build file.
I'm also using an Assembler to put the correct links in my response:
@Component
public class OrderModelAssembler
implements RepresentationModelAssembler<ProductionOrder, EntityModel<ProductionOrder>> {
@Override
public EntityModel<ProductionOrder> toModel(ProductionOrder order) {
var model = EntityModel.of(order);
switch (order.getState()) {
case DRAFT:
model.add(linkTo(methodOn(ProductionOrderController.class).rename(order.getId(), null))
.withRel(URIConstants.REL_RENAME));
model.add(linkTo(methodOn(ProductionOrderController.class).submit(order.getId()))
.withRel(URIConstants.REL_SUBMIT));
break;
case SUBMITTED:
model.add(linkTo(methodOn(ProductionOrderController.class).accept(order.getId(), null))
.withRel(URIConstants.REL_ACCEPT));
break;
case ACCEPTED:
break;
}
return model;
}
@Override
public CollectionModel<EntityModel<ProductionOrder>> toCollectionModel(
Iterable<? extends ProductionOrder> entities) {
var productionModel = StreamSupport
.stream(entities.spliterator(), false)
.map(this::toModel)
.toList();
return CollectionModel.of(productionModel,
linkTo(methodOn(ProductionOrderController.class)
.getAllOrders()).withSelfRel());
}
}
I tried to add the following bean in my main class, but with no success.
@SpringBootApplication
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@Configuration
public class ProductionOrdersApplication {
public static void main(String[] args) {
SpringApplication.run(ProductionOrdersApplication.class, args);
}
@Bean
LinkRelationProvider relProvider() {
return new LinkRelationProvider() {
@Override
public LinkRelation getItemResourceRelFor(Class<?> type) {
return LinkRelation.of("productionOrder");
}
@Override
public LinkRelation getCollectionResourceRelFor(Class<?> type) {
return LinkRelation.of("productionOrders");
}
};
}
}
I've read the section in the Hateoas guide, where the 4 possibilities are those I tried.
Can you explain how to modify the array name in the _embedded tag using the RelProvider interface in order to obtain the following result:
{
"_embedded": {
"productionOrders": [
{
"_links": {
"rename": {
"href": "http://localhost:8080/productionOrders/46ceb9a6-e6d5-4be8-9534-c1f2348b7592/rename"
},
"submit": {
"href": "http://localhost:8080/productionOrders/46ceb9a6-e6d5-4be8-9534-c1f2348b7592/submit"
}
},
"expectedCompletionDate": "1970-01-01",
"id": "46ceb9a6-e6d5-4be8-9534-c1f2348b7592",
"name": "A test order",
"state": "DRAFT"
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/productionOrders"
}
}
}
Thanks
Olivier