ResourceAssembler doesn't get marshelled correctly when sent inside Collection
See original GitHub issueI have this ResourceAssembler to insert _link to the response:
@Component
public class UserJobResourceAssembler implements ResourceAssembler<UserJob, Resource<UserJob>> {
private EntityLinks entityLinks;
public UserJobResourceAssembler(EntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
@Override
public Resource<UserJob> toResource(UserJob entity) {
Resource<UserJob> resource = new Resource<>(entity);
resource.add(
entityLinks.linkFor(UserJob.class, entity.getUser().getUniqueId()).slash(entity.getUserJobId()).withSelfRel()
);
return resource;
}
}
This works very fine if I am having a controller which returns single Resource<UserJob>
. Like below
@GetMapping(value = "/{jobId}")
public HttpEntity<Resource<UserJob>> getSingle(@PathVariable String uniqueId, @PathVariable int jobId) {
return ResponseEntity.ok( userJobService.getJob(jobId) );
}
//service
public Resource<UserJob> getJob(Integer jobId) {
return jobResourceAssembler.toResource(userJobDao.findById(jobId).orElseThrow(() -> new ResourceNotFoundException("Job not found")));
}
+++++++++++++++++++++
response:
{
"userJobId": 32,
"company": {
"id": null,
"name": "Google"
},
"designation": "Intern",
"location": "Delhi",
"started": "2016-12-31",
"ended": "2018-07-31",
"summary": "Intern at google ",
"looking_job": false,
"_links": {
"self": {
"href": "http://localhost:8085/api/v1/users/puspender-tanwar/jobs/32"
}
}
}
Notice the format of _link
.
But I am having another endpoint controller which returns me all of the jobs. Those jobs data should also contain their self
link. So I iterated over the collection returned by the Database and added the _links
using the above ResourceAssembler
@GetMapping
public HttpEntity<Set<Resource<UserJob>>> getAll(@PathVariable String uniqueId) {
return ResponseEntity.ok(userJobService.getJobs(uniqueId));
}
//service
@Override
public Set<Resource<UserJob>> getJobs(String userUniqueId) {
Set<UserJob> userJobs = userJobDao.findByUserUniqueId(userUniqueId, Sort.by(Sort.Direction.DESC, "started"));
Set<Resource<UserJob>> collectionResources = new HashSet<>();
for (UserJob userJob : userJobs) {
Resource<UserJob> jobResource = jobResourceAssembler.toResource(userJob);
collectionResources.add(jobResource);
}
return collectionResources
But the response I got this time made me confused. The format of link
is not expected in this way
[
{
"userJobId": 32,
"company": {
"id": null,
"name": "Google"
},
"designation": "Intern",
"location": "Delhi",
"started": [
2016,
12,
31
],
"ended": [
2018,
7,
31
],
"summary": "Intern at google ",
"looking_job": false,
"links": [
{
"rel": "self",
"href": "http://localhost:8085/api/v1/users/puspender-tanwar/jobs/32",
"hreflang": null,
"media": null,
"title": null,
"type": null,
"deprecation": null
}
]
},
{
//other
}
Also, started
and ended
format changes to array.
What could be the real cause of this unusual behaviour?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to correctly use PagedResourcesAssembler from Spring ...
So the PagedResourcesAssembler will take a Spring Data Page instance and transform it into a PagedResources instance by evaluating the Page ...
Read more >Spring HATEOAS - Reference Documentation
The controller indicates it's exposing collection and item resources for the entity Order . 2, Its collection resource is exposed under /orders.
Read more >How to write paginated controller that expose resource or list ...
Try to use PagedResourcesAssembler to build paged resources: @RestController @RequestMapping("persons") public class PersonController { @Autowired private ...
Read more >Fix list for IBM WebSphere Application Server V8.0
IBM WebSphere Application Server provides periodic fixes for the base and Network Deployment editions of release V8.0. The following is a complete listing ......
Read more >SCEA part 1 mock questions and Ans - Google Sites
JMS provides a common way for Java programs to create, send, receive and read an enterprise messaging system's messages. JMS defines a set...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Essentially, you must return a
ResourceSupport
object (or one of its subtypes) to get Spring HATEOAS to render HAL.You can always come up with your own subclass of
ResourceSupport
. It will produce HAL.Thanks for the help @gregturn