spring-projects / spring-hateoas

Spring HATEOAS - Library to support implementing representations for hyper-text driven REST web services.
https://spring.io/projects/spring-hateoas
Apache License 2.0
1.04k stars 477 forks source link

ResourceAssemblerSupport can't build proper links #571

Open gregturn opened 7 years ago

gregturn commented 7 years ago

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"
      }
    }
  }
gregturn commented 7 years ago

When Spring HATEOAS reaches Java 8, we should entertain method handles as the means to linkTo(methodOn()).

gregturn commented 7 years ago

@olivergierke What are the odds we could retire ResourceAssemblerSupport in favor of #591?

If not, perhaps the functionality for creating these links could be extracted into a utility function that both could leverage.