Reputation: 2393
I am trying to use below code for REST API
@GetMapping(produces = { "application/hal+json" }, value = "/topics")
public CollectionModel<Topic> getAllTopics() {
List<Topic> topics= topicService.getTopicList();
for (Topic topic : topics) {
String topicId = topic.getId();
Link selfLink = linkTo(TopicController.class).slash(topicId).withSelfRel();
topic.add(selfLink);
}
Link link = linkTo(TopicController.class).withSelfRel();
CollectionModel<Topic> result = CollectionModel.of(topics, link);
return result;
}
Output I get is below
{
"_embedded": {
"topicList": [
{
"id": "2",
"name": "rest api1",
"description": "REST API1",
"_links": {
"self": {
"href": "http://localhost:8080/2"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080"
}
}
}
Expected URL should be
{
"_embedded": {
"topicList": [
{
"id": "2",
"name": "rest api1",
"description": "REST API1",
"_links": {
"self": {
"href": "http://localhost:8080/topics/2"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/topics"
}
}
}
Upvotes: 1
Views: 566
Reputation: 2449
Modify
Link link = linkTo(TopicController.class).withSelfRel();
to
Link link = linkTo(methodOn(TopicController.class).getAllTopics()).withSelfRel();
Upvotes: 1