Skip to content

ControllerLinkBuilder.linkTo fails to build a link to a method that uses entity as a target of @PathVariable in its signature #352

@vtsukur

Description

@vtsukur

Consider a project using Spring Data JPA / Spring Data REST to manage an entity MyEntity via repository annotated with @RepositoryRestResource:

@RepositoryRestResource
public interface MyEntityRepository extends PagingAndSortingRepository<MyEntity, Long> {}

Consider the following (custom) Spring MVC controller in such an application:

@RestController
@RequestMapping("/path/{id}/subpath")
public class MyResourceController {

    @RequestMapping(method = RequestMethod.GET)
    public ... getSomething(@PathVariable("id") MyEntity myEntity) ...

}

and the following ResourceProcessor:

@Component
public class MyResourceProcessor implements ResourceProcessor<Resource<MyEntity>> {

    @Override
    public Resource<MyEntity> process(Resource<MyEntity> resource) {
        resource.add(linkTo(methodOn(MyResourceController.class).getSomething(resource.getContent())).withRel("payment"));
        return resource;
    }

}

ResourceProcessor will fail to generate the link avoiding the knowledge and capabilities of DomainClassConverter and @PathVariable

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type @org.springframework.web.bind.annotation.PathVariable org...domain.MyEntity to type java.lang.String
        at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313)
        at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
        at org.springframework.hateoas.mvc.AnnotatedParametersParameterAccessor$BoundMethodParameter.asString(AnnotatedParametersParameterAccessor.java:172)
        at org.springframework.hateoas.mvc.ControllerLinkBuilderFactory.linkTo(ControllerLinkBuilderFactory.java:130)
        at org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo(ControllerLinkBuilder.java:136)
        at org...MyResourceProcessor.process(BookingProcessor.java:25)
        at org...MyResourceProcessor.process(BookingProcessor.java:16)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions