spring-projects / spring-projects/spring-hateoas

ResourceAssemblerSupport can't build proper links

Open
#571 2 comments 0 reactions 1 assignee View on GitHub

@gregturn is already working on this.

Since Mar 31, 2017.

process: waiting for feedback type: bug
Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

By using slash(), ResourceAssemblerSupport is failing to properly take into account the full path of a Spring MVC route.

protected D createResourceWithId(Object id, T entity, Object... parameters) {

	Assert.notNull(entity, "Entity must not be null!");
	Assert.notNull(id, "Id must not be null!");

	D instance = instantiateResource(entity);
	instance.add(linkTo(controllerClass, parameters).slash(id).withSelfRel());
	return instance;
}

This fails for a controller like this:

@GetMapping("/employees/{id}")
public ResponseEntity<?> findOne(@PathVariable String id) {
	return ResponseEntity.ok(
		assembler.toResource(repository.findOne(Long.valueOf(id))));
}

In this situation, the route mapping is /employees/1, but yields this:

  {
    "id": 1,
    "lastName": "Baggins",
    "firstName": "Frodo",
    "role": "ring bearer",
    "_links": {
      "self": {
        "href": "http://localhost:8080/1"
      }
    }
  }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.